使用JavaScript计算HMAC-SHA1签名

anc*_*hor 4 javascript java amazon-web-services hmacsha1

我正在编写需要与服务器通信的JavaScript客户端应用程序。我尝试实现API,但是我坚持使用一种方法,我需要帮助。

感染我不知道如何将其从Java转换为JavaScript(我不知道在哪里可以找到用javascript编写的,用于此方法的模拟库):

import java.security.SignatureException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

/**
* This class defines common routines for generating
* authentication signatures for AWS requests.
*/
public class Signature {
 private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";


/**
* Computes RFC 2104-compliant HMAC signature.
* * @param data
* The data to be signed.
* @param key
* The signing key.
* @return
* The Base64-encoded RFC 2104-compliant HMAC signature.
* @throws
* java.security.SignatureException when signature generation fails
*/
public static String calculateRFC2104HMAC(String data, String key)
throws java.security.SignatureException
{
   String result;
   try {

     // get an hmac_sha1 key from the raw key bytes
    SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);

   // get an hmac_sha1 Mac instance and initialize with the signing key
   Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
   mac.init(signingKey);

   // compute the hmac on input data bytes
   byte[] rawHmac = mac.doFinal(data.getBytes());

   // base64-encode the hmac
   result = Encoding.EncodeBase64(rawHmac);

   } catch (Exception e) {
     throw new SignatureException("Failed to generate HMAC : " + e.getMessage());
   }
  return result;
 }
}
Run Code Online (Sandbox Code Playgroud)

此方法来自AWS文档: 用于计算HMAC-SHA1签名的Java示例代码

我在问是否有人可以给我一些参考资料(网站),以便在其中找到用JavaScript编写的解决方案或模拟库。

我在AWS文档和SDK中搜索了JavaScript,但在JS中找不到翻译。

首先十分感谢。

Mr3*_*037 6

嗨,我认为这可能对您有帮助。

在这里您可以找到用于计算hmac sha1的链接:

http://caligatio.github.io/jsSHA/

在这里,您可以找到javascript中的源代码。

https://github.com/Caligatio/jsSHA/releases/tag/v1.5.0