java 用于在 Android 上生成 HMAC-SHA1 OAuth 签名的库?

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

Library for generating HMAC-SHA1 OAuth signature on Android?

javaandroidoauthsha1hmac

提问by Will Curran

Using the specificationsbelow I need to create an oauth_signature on Android. I'm looking for a library that handles the boiler plate code in creating a signature for accessing resources via OAuth.

使用下面的规范,我需要在 Android 上创建一个 oauth_signature。我正在寻找一个库来处理样板代码以创建通过 OAuth 访问资源的签名。

  1. Construct a signature "base string", which consists of a concatenation of three request elements:

    • The HTTP request method.
    • The base URL the request is being sent to. This URL should not include any query parameters. When signing calls to Google services, refer to the OAuth specification, Section 9.1.2, for relevant instructions.
    • A normalized string of the parameters in the request (excluding the oauth_signature parameter). This includes parameters sent in the request header or body, as well as query parameters added to the request URL. To normalize the string, sort the parameters using lexicographical byte value ordering. For more details on normalizing this string, see Section 9.1.1 of the OAuth specification.
  2. Generate an oauth_signature using one of the following sequences:

    • If your application is registered and you're using HMAC-SHA1, use the OAuth "consumer secret" value generated during registration; this value is displayed on your domain's registration page.
  1. 构造一个签名“基本字符串”,它由三个请求元素的串联组成:

    • HTTP 请求方法。
    • 请求被发送到的基本 URL。此 URL 不应包含任何查询参数。签署对 Google 服务的调用时,请参阅 OAuth 规范第 9.1.2 节以获取相关说明。
    • 请求中参数的规范化字符串(不包括 oauth_signature 参数)。这包括在请求标头或正文中发送的参数,以及添加到请求 URL 的查询参数。要规范化字符串,请使用字典字节值顺序对参数进行排序。有关规范化此字符串的更多详细信息,请参阅 OAuth 规范的第 9.1.1 节。
  2. 使用以下序列之一生成 oauth_signature:

    • 如果您的应用程序已注册并且您使用的是 HMAC-SHA1,请使用注册期间生成的 OAuth“消费者机密”值;此值显示在您域的注册页面上。

采纳答案by andrewmitchell

I've used this library for an Android OAuth Client: http://code.google.com/p/oauth-signpost/

我已将此库用于 Android OAuth 客户端:http: //code.google.com/p/oauth-signpost/

回答by DEzra

In answer to Will's question on Chris's answer, you could use the built in android javax.crypto.mac to generate the hmacsha1 signature using following code (standard Java JCE provider apis):

在回答 Will 对 Chris 的回答提出的问题时,您可以使用内置的 android javax.crypto.mac 使用以下代码(标准 Java JCE 提供程序 apis)生成 hmacsha1 签名:

Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec secret = new SecretKeySpec(key.getBytes(), mac.getAlgorithm());
mac.init(secret);
byte[] digest = mac.doFinal(baseString.getBytes());
byte[] result=Base64.encode(digest, DEFAULT);

Where 'secret' would be you text you wanted to encode and 'result' above would be your hash encoded signature.

“秘密”是您想要编码的文本,上面的“结果”将是您的哈希编码签名。

回答by Muneef M

Here is the code i used, just pass the value and key to the hmacSha1().. it returns hmacsha1 string;

这是我使用的代码,只需将值和键传递给 hmacSha1().. 它返回 hmacsha1 字符串;

private static String hmacSha1(String value, String key)
            throws UnsupportedEncodingException, NoSuchAlgorithmException,
            InvalidKeyException {
        String type = "HmacSHA1";
        SecretKeySpec secret = new SecretKeySpec(key.getBytes(), type);
        Mac mac = Mac.getInstance(type);
        mac.init(secret);
        byte[] bytes = mac.doFinal(value.getBytes());
        return bytesToHex(bytes);
    }

    private final static char[] hexArray = "0123456789abcdef".toCharArray();

    private static String bytesToHex(byte[] bytes) {
        char[] hexChars = new char[bytes.length * 2];
        int v;
        for (int j = 0; j < bytes.length; j++) {
            v = bytes[j] & 0xFF;
            hexChars[j * 2] = hexArray[v >>> 4];
            hexChars[j * 2 + 1] = hexArray[v & 0x0F];
        }
        return new String(hexChars);
    }

回答by Chris Jester-Young

I don't know anything about OAuth, but you can use javax.crypto.Macto generate HMAC-SHA1 value (use HmacSHA1as the algorithm name):

我对 OAuth 一无所知,但您可以使用它javax.crypto.Mac来生成 HMAC-SHA1 值(HmacSHA1用作算法名称):

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