Login MessageDigest achieve encryption

Class MessageDigest

   抽象类:public abstract class MessageDigest extends MessageDigestSpi

      (java.security.MessageDigest,  java.security.MessageDigestSpi)

  The message digest algorithm MessageDigest class provides for the application function, such as SHA-1 or SHA-256. Message digest is secure one-way hash function that takes arbitrary-sized data, and outputs a fixed-length hash value.

  MessageDigest object starts initialization. The method using the data update processing. At any time you can call reset to reset the summary. After updating all the data to be updated, a summary of which should be called to complete the hash calculation method.

  For a given number of updates, you can call the digest method once. After the call summary, MessageDigest object is reset to its initialization state.

  Implementation is free to implement the Cloneable interface. The client application can be tested by attempting to clone and clonal capture CloneNotSupportedException .

 1 MessageDigest md = MessageDigest.getInstance("SHA-256");
 2 
 3  try {
 4      md.update(toChapter1);
 5      MessageDigest tc1 = md.clone();
 6      byte[] toChapter1Digest = tc1.digest();
 7      md.update(toChapter2);
 8      ...etc.
 9  } catch (CloneNotSupportedException cnse) {
10      throw new DigestException("couldn't make digest of partial content");
11  }

标准的MessageDigest algorithm

Constructor:

 

method:

 

example:

    private static byte[] getMessageDigest(byte[] randnum, String password) {
        //声明消息摘要对象
        MessageDigest md = null;
        try {
            //创建消息摘要
            md = MessageDigest.getInstance("MD5");
            //将随机数据传入消息摘要对象
            md.update(randnum);
            //将口令的数据传给消息摘要对象
            md.update(password.getBytes("UTF-8"));
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch(UnsupportedEncodingException E) { 
            e.printStackTrace (); 
        } 
        // get the message digest array of bytes 
        return md.digest (); 
    }

 

 

Details can be found  https://docs.oracle.com/javase/8/docs/api/java/security/MessageDigest.html?is-external=true

 

Guess you like

Origin www.cnblogs.com/zzm96/p/12443729.html