-
-
Save yaoxinghuo/6265845 to your computer and use it in GitHub Desktop.
SHA1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.terrynow.common; | |
import java.security.MessageDigest; | |
import java.security.NoSuchAlgorithmException; | |
import java.util.Locale; | |
/** | |
* @author Terry E-mail: yaoxinghuo at 126 dot com | |
* @date Aug 19, 2013 9:54:05 AM | |
*/ | |
public class SHA1 { | |
public static String sha1(String inStr) { | |
MessageDigest md = null; | |
String outStr = null; | |
try { | |
md = MessageDigest.getInstance("SHA-1"); // 选择SHA-1,也可以选择MD5 | |
byte[] digest = md.digest(inStr.getBytes()); // 返回的是byet[],要转化为String存储比较方便 | |
outStr = bytetoString(digest); | |
} catch (NoSuchAlgorithmException nsae) { | |
nsae.printStackTrace(); | |
} | |
return outStr; | |
} | |
public static String bytetoString(byte[] digest) { | |
String str = ""; | |
String tempStr = ""; | |
for (int i = 1; i < digest.length; i++) { | |
tempStr = (Integer.toHexString(digest[i] & 0xff)); | |
if (tempStr.length() == 1) { | |
str = str + "0" + tempStr; | |
} else { | |
str = str + tempStr; | |
} | |
} | |
return str.toLowerCase(Locale.ENGLISH); | |
} | |
public static void main(String[] args) { | |
System.out.println(sha1("123456")); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment