Java Cryptographic Tools Class (dependent: Java.security.MessageDigest or org.apache.commons.codec.digest.DigestUtils)

Source: Internet
Author: User
Tags sha1

Relies on java.security.MessageDigest, supports md5,sha-1,sha-256

1 Importjava.security.MessageDigest;2 Importjava.security.NoSuchAlgorithmException;3 4 /**5 * Ciphertextutil6  *7  * @authorYSJ8  */9  Public classCiphertextutil {Ten      Public Static FinalString MD5 = "MD5"; One      Public Static FinalString sha_1 = "SHA-1"; A      Public Static FinalString sha_256 = "SHA-256"; -     Private Static Final Char[] Ch_hex = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', -' 9 ', ' A ', ' B ', ' C ', ' D ', ' E ', ' F '}; the  -     /** - * Encrypt string -      *  +      * @paramSourcestr need to encrypt the target string -      * @paramalgorithmsname algorithm name (for example: md5,sha-1,sha-256) +      * @return A      */ at      Public Staticstring passalgorithmsciphering (String sourcestr,string algorithmsname) { -String Password = ""; - MessageDigest MD; -         Try { -MD =messagedigest.getinstance (algorithmsname); -             //Update summary using specified byte[] in md.update (Sourcestr.getbytes ()); -             //complete the calculation, return the result array to             byte[] B =md.digest (); +Password =Bytearraytohex (b); -}Catch(nosuchalgorithmexception e) { the e.printstacktrace (); *         } $         returnpassword;Panax Notoginseng     } -  the     /** + * Convert byte array to hexadecimal string A      * the      * @parambytes +      * @returnreturns a 16 binary string -      */ $     Private StaticString Bytearraytohex (byte[] bytes) { $         //One byte is 8 bits, one hexadecimal character occupies 4 bits, and the hexadecimal character array is twice times the length of the byte array . -         Char[] chars =New Char[Bytes.length * 2]; -         intindex = 0; the          for(byteb:bytes) { -             //High 4 bits of bytes takenWuyichars[index++] = ch_hex[b >>> 4 & 0xf]; the             //take the lower 4 bits of the byte -chars[index++] = ch_hex[b & 0xf]; Wu         } -         return NewString (chars); About     } $}

Relies on org.apache.commons.codec.digest.DigestUtils, supports md2,md5,sha-1,sha-256,sha-384,sha-512

apache:http://commons.apache.org/proper/commons-codec/download_codec.cgi

1 import org.apache.commons.codec.digest.DigestUtils;2 3 /**4 * Ciphertextutil5  *6 * @author YSJ7  */8  Public classCiphertextutil {9      Public StaticFinal String MD2 ="MD2";Ten      Public StaticFinal String MD5 ="MD5"; One      Public StaticFinal String sha_1 ="SHA1"; A      Public StaticFinal String sha_256 ="SHA256"; -      Public StaticFinal String sha_384 ="SHA384"; -      Public StaticFinal String sha_512 ="SHA512"; the  -     /** - * Encrypt string -      *  + * @param sourcestr need to encrypt the target string - * @param algorithmsname algorithm name (for example: md2,md5,sha1,sha256,sha384,sha512) + * @return A      */ at      Public Staticstring passalgorithmsciphering (String sourcestr,string algorithmsname) { -String Password =""; -         Switch(algorithmsname) { -          Case "MD2": -Password =Digestutils.md2hex (SOURCESTR); -              Break; in          Case "MD5": -Password =Digestutils.md5hex (SOURCESTR); to              Break; +          Case "SHA1": -Password =Digestutils.sha1hex (SOURCESTR); the              Break; *          Case "SHA256": $Password =Digestutils.sha256hex (SOURCESTR);Panax Notoginseng              Break; -          Case "SHA384": thePassword =Digestutils.sha384hex (SOURCESTR); +              Break; A          Case "SHA512": thePassword =Digestutils.sha512hex (SOURCESTR); +              Break; -         } $         returnpassword; $     } -}

Java Cryptographic Tools Class (dependent: Java.security.MessageDigest or org.apache.commons.codec.digest.DigestUtils)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.