Is sun.misc package still available in java?

I would like to use the Base64 encoder of the package sun.misc.BASE64Encoder, because I need to encode the password. However, an error occurs while entering the import for this package.

The message in which the code for the encoder should be used is as follows:

private synchronized static String hash(final String username, final String password) { DIGEST.reset(); return new BASE64Encoder().encode(DIGEST.digest((username.toLowerCase() + password).getBytes())); } 

Is there an equivalent class in java that does the same thing? Or maybe someone knows how to get the code of the original class, maybe please?

thanks:)

+4
source share
5 answers

I suggest you forget about sun.misc.BASE64Encoder and use the Apache Commons Base64 class. Here is the link: http://commons.apache.org/codec/apidocs/org/apache/commons/codec/binary/Base64.html


Update (6/7/2017)

Using sun.misc.BASE64Encoder will result in a compilation error with Java 9 and already gives a warning in Java 8.

The correct class to use is Base64 in the java.util package.

Example:

 import java.util.Base64; Base64.getDecoder().decode(...); 
+2
source

There is no "public" (so to speak) class in Java for this. Consider using the Apache Commons Codec package, which contains a base64 implementation. The homepage is here: http://commons.apache.org/codec/ .

You use the sun.* Or com.sun.* directly at your own risk. Backward compatibility is not guaranteed for them.

+6
source

See this question , but IMHO the accepted answer is incorrect. You never want to use paperless classes, like everyone else starting with sun . The reason for this is that now your code depends on the specific JVM implementation (for example, the JVM for IBM may not have this).

The second answer (with the most votes) is the one you want.

+3
source

There is a big difference between warning and error, so you need to read the message carefully to find out what it is.

Classes in sun.misc may vary and may not be present on all JVMs. You get using a library encoded with BASE64.

IMHO you can use sun.misc, do you understand all the risks associated with the class. However, in your case it is much better to avoid using any class in sun. and com.sun.

+2
source

In general, Oracle recommends not calling packages "Sun"

http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html

In addition, in your case, there was a request to transfer the use of sun.misc. * to supported APIs (JDK-8066506), and it is associated with sun.misc.BASE64Decoder:

 sun.misc.BASE64Decoder, sun.misc.BASE64Encoder Use java.util.Base64 @since 8 instead 

source: http://www.oracle.com/technetwork/java/faq-sun-packages-142232.html

0
source

Source: https://habr.com/ru/post/1388065/


All Articles