Implementation of hash SHA1 and MD5 under Java/android

Source: Internet
Author: User
Tags md5 encryption

Android MD5 encryption is identical to the J2SE platform because the Android platform supports java.security.MessageDigest this package. is actually exactly the same as the J2SE platform.

Algorithm Signature:

[Java]View Plaincopy
    1. String GetMD5 (String val) throws NoSuchAlgorithmException

Enter a string (text that needs to be encrypted) to get an encrypted output string (encrypted text)

[Java]View Plaincopy
  1. Package com.tencent.utils;
  2. Import Java.security.MessageDigest;
  3. Import java.security.NoSuchAlgorithmException;
  4. /**
  5. * GETMD5 (String) method available externally
  6. * @author Randyjia
  7. *
  8. */
  9. Public class MD5 {
  10. public static string GetMD5 (string val) throws nosuchalgorithmexception{
  11. MessageDigest MD5 = messagedigest.getinstance ("MD5");
  12. Md5.update (Val.getbytes ());
  13. byte[] m = md5.digest (); //Encryption
  14. return getString (m);
  15. }
  16. private static String getString (byte[] b) {
  17. StringBuffer sb = new StringBuffer ();
  18. For (int i = 0; i < b.length; i + +) {
  19. Sb.append (B[i]);
  20. }
  21. return sb.tostring ();
  22. }
  23. }

SHA-1

        NULL ;         Try {            digester  = messagedigest.getinstance ("SHA-1");         Catch (nosuchalgorithmexception e) {            //  TODO auto-generated catch block             E.printstacktrace ();        }        Digester.update (calhashdata);         byte [] Hashd = Digester.digest ();    

Implementation of hash SHA1 and MD5 under Java/android

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.