How to parse an example of Java coding digest algorithm
This article shows you how to parse the Java coding summary algorithm examples, the content is concise and easy to understand, absolutely can make your eyes bright, through the detailed introduction of this article, I hope you can get something.
URL encoding and decoding
String s = "Hello, World!" ; / / URL encoding String urlEncodedString = URLEncoder.encode (s, "UTF-8"); / / URL decoding String urlDecodedString = URLDecoder.decode (urlEncodedString, "UTF-8")
Base64 encoding and decoding
/ / Base64 Encoding String base64EncodedString = Base64 .getEncoder () / = .withoutPadding () after removing Base64. EncodeToString ("Hello, World!!" .getBytes (StandardCharsets.UTF_8); / / Base64 decoding byte [] base64DecodedByteArray = Base64.getDecoder () .decode (base64EncodedString); String base64DecodedString = new String (base64DecodedByteArray, StandardCharsets.UTF_8)
Byte [] to hexadecimal string
The following summary algorithm will be used to visualize the results.
/ * * byte [] bitwise conversion to hexadecimal String * @ param aData byte [] * @ return byte [] bitwise conversion to hexadecimal String * / public static String byteArrayToHexString (byte [] aData) {final char [] hexChars = "0123456789abcdef" .toCharArray (); / / A byte represents StringBuilder stringBuilder = new StringBuilder (aData.length * 2) with two hexadecimal characters For (byte b: aData) {/ / High 4 bits converted to hexadecimal stringBuilder.append (hexChars [(b > 4) & 0x0f]); / / low 4 bits converted to hexadecimal stringBuilder.append (Hexchars [b & 0x0f]);} return stringBuilder.toString ();}
MD5 algorithm
/ / the parameter is the name of the summary algorithm MessageDigest messageDigest = MessageDigest.getInstance ("MD5"); / / the following two lines of code are equivalent to messageDigest.update ("Hello, World!" .getBytes (StandardCharsets.UTF_8); messageDigest.update ("Hello," .getBytes (StandardCharsets.UTF_8)); messageDigest.update ("World!" .getBytes (StandardCharsets.UTF_8); byte [] md5Bytes = messageDigest.digest ()
SHA1 algorithm
/ / SHA256 just change the name of the algorithm MessageDigest messageDigest = MessageDigest.getInstance ("SHA1"); / / the following two lines of code are equivalent to messageDigest.update ("Hello, World!" .getBytes (StandardCharsets.UTF_8); messageDigest.update ("Hello," .getBytes (StandardCharsets.UTF_8)); messageDigest.update ("World!" .getBytes (StandardCharsets.UTF_8); byte [] md5Bytes = messageDigest.digest ()
In this paper, the sample code is introduced in great detail, which has a certain reference value for everyone's study or work.
The above content is how to parse the example of Java coding summary algorithm. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.