java 在 Android 上进行 SHA1 加密?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5757024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Make SHA1 encryption on Android?
提问by Ferdinand
Can you suggest me about how to encrypt string using SHA1 algorithm ? I've searched about it. But no luck.
你能建议我如何使用 SHA1 算法加密字符串吗?我已经搜索过了。但没有运气。
Thanks in advance.
提前致谢。
回答by Mikael Engver
binnyb's convertToHex
method is not working properly. A more correct one that works for me is:
binnyb 的convertToHex
方法不能正常工作。对我有用的更正确的方法是:
private static String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9)) {
buf.append((char) ('0' + halfbyte));
}
else {
buf.append((char) ('a' + (halfbyte - 10)));
}
halfbyte = data[i] & 0x0F;
} while(two_halfs++ < 1);
}
return buf.toString();
}
public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] sha1hash = new byte[40];
md.update(text.getBytes("iso-8859-1"), 0, text.length());
sha1hash = md.digest();
return convertToHex(sha1hash);
}
use the SHA1
method to get your sha1 string.
使用该SHA1
方法获取您的 sha1 字符串。
Update: providing a complete answer
更新:提供完整的答案
回答by james
here are 2 methods i have found while searching for a sha1 algorithm implementation:
这是我在搜索 sha1 算法实现时发现的两种方法:
private static String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
int length = data.length;
for(int i = 0; i < length; ++i) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do {
if((0 <= halfbyte) && (halfbyte <= 9))
buf.append((char) ('0' + halfbyte));
else
buf.append((char) ('a' + (halfbyte - 10)));
halfbyte = data[i] & 0x0F;
}
while(++two_halfs < 1);
}
return buf.toString();
}
public static String SHA1(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest md = MessageDigest.getInstance("SHA-1");
byte[] sha1hash = new byte[40];
md.update(text.getBytes("iso-8859-1"), 0, text.length());
sha1hash = md.digest();
return convertToHex(sha1hash);
}
use the SHA1
method to get your sha1 string. I have not confirmed that this is indeed a sha1, but it works for my apps.
使用该SHA1
方法获取您的 sha1 字符串。我还没有确认这确实是一个 sha1,但它适用于我的应用程序。
回答by Risadinha
I've answered this before (How to SHA1 hash a string in Android?) but it fits here, as well:
我之前回答过这个问题(How to SHA1 hash a string in Android?)但它也适合这里:
Android comes with Apache's Commons Codec so you can simply use the following line to create a SHA-1 hexed String:
Android 随附 Apache 的 Commons Codec,因此您只需使用以下行来创建 SHA-1 十六进制字符串:
String myHexHash = DigestUtils.shaHex(myFancyInput);
That is the old deprecated method you get with Android 4 by default. The new versions of DigestUtils bring all flavors of shaHex() methods like sha256Hex() and also overload the methods with different argument types.
这是默认情况下您在 Android 4 中获得的旧的弃用方法。DigestUtils 的新版本带来了所有风格的 shaHex() 方法,如 sha256Hex() 并且还重载了具有不同参数类型的方法。
Of course, there is more functionality in DigestUtils and the rest of Commons Codec. Just have a look.
当然,DigestUtils 和 Commons Codec 的其余部分还有更多功能。看看吧。
EDIT:
编辑:
If you get a ClassNotFoundError you will have to explicitly add commons-codec as dependency (even though it should come with Android as transitive dependency), in Maven e.g.:
如果您收到 ClassNotFoundError,则必须在 Maven 中明确添加 commons-codec 作为依赖项(即使它应该与 Android 作为传递依赖项一起提供),例如:
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.7</version>
</dependency>
And also, you will have to change the call to:
而且,您必须将呼叫更改为:
String myHexHash = new String(Hex.encodeHex(DigestUtils.sha512(myFancyInput)));
(My humble guess is that this is probably due to a ClassLoader issue (class name collision) in the Android VM - which would actually prove that the commons-codec classes are already present...)
(我粗浅的猜测是,这可能是由于 Android VM 中的 ClassLoader 问题(类名冲突)造成的——这实际上证明了 commons-codec 类已经存在......)
回答by Ruben
binnyb set me on the right track, but I found some more, easier to understand code here: http://www.coderanch.com/t/526487/java/java/Java-Byte-Hex-String
binnyb 让我走上正轨,但我在这里找到了更多更容易理解的代码:http: //www.coderanch.com/t/526487/java/java/Java-Byte-Hex-String
private static String convertToHex(byte[] data) {
StringBuilder sb = new StringBuilder(data.length * 2);
Formatter fmt = new Formatter(sb);
for (byte b : data) {
fmt.format("%02x", b);
}
return sb.toString();
}