Created
October 14, 2013 06:43
-
-
Save daichan4649/6971718 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
private static final String ALGORITHM_SHA1 = "SHA1"; | |
private static final String SALT = "saltvalue"; | |
public static String encode2SHA1Text(String plainText) { | |
return encode2SHA1Text(plainText, ALGORITHM_SHA1, SALT); | |
} | |
public static String encode2SHA1Text(String plainText, String algorithm, String salt) throws NoSuchAlgorithmException { | |
MessageDigest md = MessageDigest.getInstance(algorithm); | |
// salt+plainText | |
final String target = String.format("%s%s", salt, plainText); | |
md.update(target.getBytes(), 0, target.getBytes().length); | |
final byte[] sha1hash = md.digest(); | |
StringBuilder encodedText = new StringBuilder(); | |
for (int i = 0; i < sha1hash.length; i++) { | |
encodedText.append(String.format("%02x", sha1hash[i])); | |
} | |
return encodedText.toString(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment