Java SHA1 sha1(String text)

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

Description

sha

License

Apache License

Declaration

public static String sha1(String text) 

Method Source Code


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

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

public class Main {

    public static String sha1(String text) {
        MessageDigest md = null;//from ww  w.  j ava 2s .  co  m
        String outStr = null;
        try {
            md = MessageDigest.getInstance("SHA-1");
            byte[] digest = md.digest(text.getBytes());
            outStr = bytes2hex(digest);
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e);
        }
        return outStr;
    }

    public static String bytes2hex(byte[] bytes) {
        StringBuilder hex = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            byte b = bytes[i];
            boolean negative = false;
            if (b < 0)
                negative = true;
            int bAbs = Math.abs(b);
            if (negative)
                bAbs = bAbs | 0x80;
            String temp = Integer.toHexString(bAbs & 0xFF);
            if (temp.length() == 1) {
                hex.append("0");
            }
            hex.append(temp.toLowerCase());
        }
        return hex.toString();
    }
}

Related

  1. sha1(String str)
  2. SHA1(String str)
  3. sha1(String string)
  4. sha1(String string)
  5. SHA1(String strs)
  6. SHA1(String text)
  7. sha1(String text)
  8. SHA1(String text)
  9. SHA1(String text)