Java SHA1 sha1(String s)

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

Description

sha

License

Apache License

Declaration

public static String sha1(String s) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class Main {
    static final char[] HEX_CHARS = "0123456789abcdef".toCharArray();

    public static String sha1(String s) {
        MessageDigest md = getMessageDigest("sha-1");
        return bytes2HexString(md.digest(s.getBytes(StandardCharsets.UTF_8)));
    }/*  w ww  .java 2 s  .co m*/

    static MessageDigest getMessageDigest(String name) {
        try {
            return MessageDigest.getInstance(name);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
    }

    static String bytes2HexString(byte[] b) {
        StringBuilder sb = new StringBuilder(b.length << 2);
        for (byte x : b) {
            int hi = (x & 0xf0) >> 4;
            int lo = x & 0x0f;
            sb.append(HEX_CHARS[hi]);
            sb.append(HEX_CHARS[lo]);
        }
        return sb.toString();
    }
}

Related

  1. sha1(String raw)
  2. sha1(String s)
  3. sha1(String s)
  4. sha1(String s)
  5. sha1(String s)
  6. sha1(String s)
  7. sha1(String s)
  8. sha1(String src)
  9. sha1(String src)