是否每部 Android 手机都支持 SHA-256

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10129311/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:36:27  来源:igfitidea点击:

Does every Android phone support SHA-256

androidhashshamessage-digest

提问by joshkendrick

So reading this post: How can I calculate the SHA-256 hash of a string in Android?

所以阅读这篇文章:如何计算 Android 中字符串的 SHA-256 哈希?

and the docs: http://developer.android.com/reference/java/security/MessageDigest.html

和文档:http: //developer.android.com/reference/java/security/MessageDigest.html

I'm curious; which phones will support SHA-256? In the docs, the line about the 'NoSuchAlgorithmException' makes me think that some phones don't support all algorithms. Before I go implementing this for an app and expecting it to work the same on all phones I want to know if anyone knows anything about this...?

我很好奇; 哪些手机将支持 SHA-256?在文档中,关于“NoSuchAlgorithmException”的那一行让我觉得有些手机并不支持所有算法。在我为一个应用程序实现这个并期望它在所有手机上都能正常工作之前,我想知道是否有人对此有所了解......?

I find it strange that the MessageDigest class doesn't have some constants to pick the algorithm you want to use.

我觉得奇怪的是 MessageDigest 类没有一些常量来选择你想要使用的算法。

采纳答案by Chris Cashwell

All Android devices support SHA-256. The NoSuchAlgorithmExceptionindicates that a requested algorithm could not be found and is necessary because the method takes a Stringargument for the algorithm name. If you passed in "foo-256", the method's only recourse is to throw a NoSuchAlgorithmExceptionbecause, for reasons beyond my understanding, there's no algorithm called "foo-256". Assuming you're passing in a name you're sure is an algorithm that Android can use, you'll never see that exception.

所有 Android 设备都支持 SHA-256。该NoSuchAlgorithmException指示请求的算法无法找到,是必要的,因为该方法需要String对算法名称的说法。如果您传入“foo-256”,该方法唯一的办法就是抛出a NoSuchAlgorithmException,因为出于我无法理解的原因,没有称为“foo-256”的算法。假设您传入一个名称,您确定该名称是 Android 可以使用的算法,那么您将永远不会看到该异常。

回答by Soheil

Add NoSuchAlgorithmException as below:

添加 NoSuchAlgorithmException 如下:

public static String SHA256 (String text) throws NoSuchAlgorithmException {

    MessageDigest md = MessageDigest.getInstance("SHA-256");

    md.update(text.getBytes());
    byte[] digest = md.digest();

    return Base64.encodeToString(digest, Base64.DEFAULT);
}

回答by garnet

SHA-256withRSA is NOT supported in older android versions (verified the same in Android 4.0.3, 4.1.1). I have experienced this problem while using JSCEP. The digest algorithm returned by SCEP server is SHA-256. But SHA-256withRSA is not present in any default SecurityProviders in those android versions. Found a relevant link: Which versions of Android support which package signing algorithms?

旧版 android 不支持 SHA-256withRSA(在 Android 4.0.3、4.1.1 中验证相同)。我在使用 JSCEP 时遇到过这个问题。SCEP 服务器返回的摘要算法是 SHA-256。但是 SHA-256withRSA 在这些 android 版本的任何默认 SecurityProvider 中都不存在。找到相关链接: 哪些版本的 Android 支持哪些包签名算法?

This link shows that SHA-256withRSA was added later: https://android-review.googlesource.com/44360

此链接显示后来添加了 SHA-256withRSA:https://android-review.googlesource.com/44360

回答by Megaetron

According to the Android Documentations for MessageDigest, SHA-256 is supported since API 1.

根据MessageDigest的 Android 文档,自 API 1 起就支持 SHA-256。

enter image description here

在此处输入图片说明