All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.leancloud.codec.SHA1 Maven / Gradle / Ivy

package cn.leancloud.codec;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class SHA1 {
  private static String convertToHex(byte[] data) {
    StringBuilder buf = new StringBuilder();
    for (byte b : data) {
      int halfbyte = (b >>> 4) & 0x0F;
      int two_halfs = 0;
      do {
        buf.append((0 <= halfbyte) && (halfbyte <= 9)
                ? (char) ('0' + halfbyte)
                : (char) ('a' + (halfbyte - 10)));
        halfbyte = b & 0x0F;
      } while (two_halfs++ < 1);
    }
    return buf.toString();
  }

  public static String compute(byte[] data) throws NoSuchAlgorithmException {
    MessageDigest md = MessageDigest.getInstance("SHA-1");
    md.update(data, 0, data.length);
    byte[] sha1hash = md.digest();
    return convertToHex(sha1hash);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy