The code collation of Sha secure hashing encryption algorithm instead of MD5 encryption

Source: Internet
Author: User
Tags md5 encryption

Based on previous experience, we usually use the MD5 encryption algorithm, although it can be cracked, but because of its cyclical longer, so I have been using, today learned a new encryption algorithm, secure hashing algorithm, specifically he is not the data encryption algorithm, but the data digital Signature Algorithm , It is not to encrypt the entire data, but rather to sign the data extraction part of the information, using the algorithm to encrypt different data, you will get a unique string, like a signature, only that data can produce such a signature string.

The following is the Java implementation of the SHA algorithm encryption , we can use this algorithm to write cryptographic tool classes:

Package Com.cloud.skater.util;import Java.security.messagedigest;import Java.util.logging.level;import  Java.util.logging.logger;public class Encrypt {private static final String Key_sha = "SHA";p rivate static final Logger log = Logger.getlogger (Encrypt.class.getName ());p ublic static string Encryptsha (String data) throws Exception { MessageDigest sha = Messagedigest.getinstance (Key_sha); Sha.update (Data.getbytes ()); return bytearraytohexstring ( Sha.digest ());} Converts a byte array to a hexadecimal string private static string Bytearraytohexstring (byte[] bytearray) {String strdigest = ""; for (int i = 0; i < bytearray.length; i++) {strdigest + = bytetohexstring (Bytearray[i]);} return strdigest;} Convert bytes to hexadecimal string private static string Bytetohexstring (Byte ib) {char[] Digit = {' 0 ', ' 1 ', ' 2 ', ' 3 ', ' 4 ', ' 5 ', ' 6 ', ' 7 ', ' 8 ', ' 9 ', ' A ', ' B ', ' C ', ' d ', ' e ', ' F '};char[] ob = new Char[2];ob[0] = digit[(ib >>> 4) & 0x0f];ob[1] = Digit [IB & 0X0F]; string s = new string (OB); return s;} public static void Main (String argS[]) {try {System.out.println (Encrypt.encryptsha ("zhang1234")),} catch (Exception e) {log.log (Level.severe, E.getmessage (), E.getcause ());}}}
using it, you can paste directly as a tool class.

The code collation of Sha secure hashing encryption algorithm instead of MD5 encryption

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.