Android API 23 中的 HmacSHA256 算法 [英] HmacSHA256 algorithm in Android API 23

查看:129
本文介绍了Android API 23 中的 HmacSHA256 算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的 Android 应用程序 (API 23) 中生成哈希值.我跟着这个链接 -

<块引用>

java.security.NoSuchAlgorithmException

我从其他 Stackoverflow 帖子中搜索并尝试了一些解决方案,但它们没有奏效.

试过这个 - MessageDigest digest = MessageDigest.getInstance("SHA-256"); 得到同样的错误.

我的总体意图是在 Java 中转换以下 C# 代码,以便我可以在我的 Android 应用程序中使用它-

string GenerateAuthToken(string verb, string resourceType, string resourceId, string date, string key, string keyType, string tokenVersion){var hmacSha256 = new System.Security.Cryptography.HMACSHA256 { Key = Convert.FromBase64String(key) };动词 = 动词 ??"";资源类型 = 资源类型 ??"";资源 ID = 资源 ID ??"";string payLoad = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}\n{1}\n{2}\n{3}\n{4}\n",动词.ToLowerInvariant(),resourceType.ToLowerInvariant(),资源 ID,date.ToLowerInvariant(),");byte[] hashPayLoad = hmacSha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(payLoad));字符串签名 = Convert.ToBase64String(hashPayLoad);return System.Web.HttpUtility.UrlEncode(String.Format(System.Globalization.CultureInfo.InvariantCulture, "type={0}&ver={1}&sig={2}",键类型,令牌版本,签名));}

所以我只是一步一步手动转换每一行并卡在这一点上.我有什么想法可以使这项工作?

解决方案

您输入的算法有误是 HmacSHA256 不是 hmacSHA256

选择算法时需要小心,因为它们区分大小写.

从你的快照我可以看到你使用了

Mac hmacSHA256 = Mac.getInstance("hmacSHA256");

这是不正确的,因为您正在尝试获取不存在的 hmacSHA256 实例!

正确的是

Mac hmacSHA256 = Mac.getInstance("HmacSHA245");

第一个H应该在大写

I am trying to generate hash value in my Android app (API 23). I followed this link- https://developer.android.com/reference/javax/crypto/Mac.html and below code should work as per it.

Mac hmacSha256 = Mac.getInstance("HmacSHA1");

But this gives compile time error-

java.security.NoSuchAlgorithmException

I searched and tried few solutions from across other Stackoverflow posts but they didn't work.

Tried this- MessageDigest digest = MessageDigest.getInstance("SHA-256"); got same error.

My overall intention is to convert the below C# code in Java so I can use it in my Android app-

string GenerateAuthToken(string verb, string resourceType, string resourceId, string date, string key, string keyType, string tokenVersion)
{
    var hmacSha256 = new System.Security.Cryptography.HMACSHA256 { Key = Convert.FromBase64String(key) };

    verb = verb ?? "";
    resourceType = resourceType ?? "";
    resourceId = resourceId ?? "";

    string payLoad = string.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}\n{1}\n{2}\n{3}\n{4}\n",
            verb.ToLowerInvariant(),
            resourceType.ToLowerInvariant(),
            resourceId,
            date.ToLowerInvariant(),
            ""
    );

    byte[] hashPayLoad = hmacSha256.ComputeHash(System.Text.Encoding.UTF8.GetBytes(payLoad));
    string signature = Convert.ToBase64String(hashPayLoad);

    return System.Web.HttpUtility.UrlEncode(String.Format(System.Globalization.CultureInfo.InvariantCulture, "type={0}&ver={1}&sig={2}",
        keyType,
        tokenVersion,
        signature));
}

So I am just going and converting each line manually step by step and stuck at this point. Any ideas I can make this work?

解决方案

You typed the algorithm incorrectly It's HmacSHA256 not hmacSHA256

You need to be careful when choosing the algorithms because they're case sensitive.

From your snapshot I can see that you used

Mac hmacSHA256 = Mac.getInstance("hmacSHA256");

It's incorrect because you're trying to get the instance of hmacSHA256 witch does not exists!

The correct one would be

Mac hmacSHA256 = Mac.getInstance("HmacSHA245");

The first H should be on caps

这篇关于Android API 23 中的 HmacSHA256 算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆