Java SHA1 sha1(String raw)

Here you can find the source of sha1(String raw)

Description

sha

License

Open Source License

Declaration

public static String sha1(String raw) 

Method Source Code


//package com.java2s;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    public static String sha1(String raw) {
        try {//from  w w  w .j av  a2  s  .  c  o  m
            MessageDigest messageDigest = MessageDigest.getInstance("SHA1");
            messageDigest.update(raw.getBytes());
            return bytesToHexStr(messageDigest.digest());
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException("sha1 encryption error", e);
        }
    }

    private static String bytesToHexStr(byte[] bytes) {
        StringBuilder stringBuilder = new StringBuilder();
        for (byte b : bytes) {
            stringBuilder.append(byteToHexStr(b));
        }
        return stringBuilder.toString();
    }

    private static String byteToHexStr(byte b) {
        char[] digit = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
        char[] tempArr = new char[2];
        tempArr[0] = digit[(b >>> 4) & 0x0F];
        tempArr[1] = digit[b & 0x0F];
        return new String(tempArr);
    }
}

Related

  1. sha1(String input)
  2. sha1(String input)
  3. sha1(String input, String encoding)
  4. sha1(String message)
  5. sha1(String param)
  6. sha1(String s)
  7. sha1(String s)
  8. sha1(String s)
  9. sha1(String s)