Digest string : Message Digest « Security « Java






Digest string

   
import java.io.IOException;
import java.security.GeneralSecurityException;
import java.security.MessageDigest;

public class Utils {
  public static String digest(String token) throws GeneralSecurityException, IOException {
    MessageDigest sha1 = MessageDigest.getInstance("SHA1");
    return byteArrayToHexStr(sha1.digest(token.getBytes("UTF-8")));
  }

  private static String byteArrayToHexStr(byte[] data) {
    char[] chars = new char[data.length * 2];
    for (int i = 0; i < data.length; i++) {
      byte current = data[i];
      int hi = (current & 0xF0) >> 4;
      int lo = current & 0x0F;
      chars[2 * i] = (char) (hi < 10 ? ('0' + hi) : ('A' + hi - 10));
      chars[2 * i + 1] = (char) (lo < 10 ? ('0' + lo) : ('A' + lo - 10));
    }
    return new String(chars);
  }
}

   
    
    
  








Related examples in the same category

1.MessageDigest with SHA-1
2.Create a checksum
3.Operations to simplifiy common java.security.MessageDigest tasks.
4.MD4 Digest
5.MD5 Digest
6.Implements the MD4 message digest algorithm in Java
7.This program computes the message digest of a file or the contents of a text area.This program computes the message digest of a file or the contents of a text area.
8.Returns the type MessageDigest of the input text with the input salt.
9.Message Digest Util
10.A directive is a parameter of the digest authentication process