javascript 如何构建签名?

发布于 2024-11-18 14:23:30 字数 720 浏览 0 评论 0原文

我有这个伪代码,我想将其转换为工作代码:

string constructSignature(string timestamp, string UID, string secretKey) {  
  baseString = timestamp + "_" + UID;                                  // Construct a "base string" for signing  
  binaryBaseString = ConvertUTF8ToBytes(baseString);    // Convert the base string into a binary array  
  binaryKey = ConvertFromBase64ToBytes(secretKey);     // Convert secretKey from BASE64 to a binary array  
  binarySignature = hmacsha1(binaryKey, baseString);      // Use the HMAC-SHA1 algorithm to calculate the signature  
  signature = ConvertToBase64(binarySignature);              // Convert the signature to a BASE64  
  return signature;  
} 

有什么想法吗? 谢谢

i have this pseudo code and i would like to convert it into working code:

string constructSignature(string timestamp, string UID, string secretKey) {  
  baseString = timestamp + "_" + UID;                                  // Construct a "base string" for signing  
  binaryBaseString = ConvertUTF8ToBytes(baseString);    // Convert the base string into a binary array  
  binaryKey = ConvertFromBase64ToBytes(secretKey);     // Convert secretKey from BASE64 to a binary array  
  binarySignature = hmacsha1(binaryKey, baseString);      // Use the HMAC-SHA1 algorithm to calculate the signature  
  signature = ConvertToBase64(binarySignature);              // Convert the signature to a BASE64  
  return signature;  
} 

any idea?
thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小嗷兮 2024-11-25 14:23:30

一些想法:

  • 不要计算 BinaryBaseString,因为你从来没有使用过它
  • 不要无缘无故地引用 UTF8 —— JavaScript 中的字符串是 Unicode,它与 UTF 的关系只是遥远的联系。
  • 不要将实现细节放入伪代码中——尤其是那些您实际上不需要的实现细节(例如假设密钥应该以“字节”为单位,无论这意味着什么,但事实上, 标准库 接受并返回字符串)。
  • 不要编写只是重述代码正在做什么的注释。

这给我们留下了:

var constructSignature = function(timestamp, UID, secretKey) {  
  return Crypto.HMAC(Crypto.SHA1, timestamp + "_" + UID, secretKey,
                     { asString: true }); 
};

Some ideas:

  • Don't calculate BinaryBaseString, given that you never use it
  • Don't refer to UTF8 for no reason at all -- strings in JavaScript are Unicode, which is only distantly connected to UTF.
  • Don't put implementation details in your pseudocode -- especially ones you don't actually need (like assuming the secret key should be in "bytes", whatever that means, when in fact, the standard library takes and returns strings).
  • Don't write comments that just restate what the code is doing.

Which leaves us with:

var constructSignature = function(timestamp, UID, secretKey) {  
  return Crypto.HMAC(Crypto.SHA1, timestamp + "_" + UID, secretKey,
                     { asString: true }); 
};
待"谢繁草 2024-11-25 14:23:30

这是您的代码的完整实现:

let atob = function(a){
    return new Buffer(a, 'base64').toString('binary');
} 

function ConvertUTF8ToBytes(str) {
    var utf8 = unescape(encodeURIComponent(str));
    var arr = [];
    for (var i = 0; i < utf8.length; i++) {
        arr.push(utf8.charCodeAt(i));
    }
    return new Uint8Array(arr);
}

let ConvertFromBase64ToBytes = function (b64Data){
    var byteCharacters = atob(b64Data);
    var byteNumbers = new Array(byteCharacters.length);
    for (var i = 0; i < byteCharacters.length; i++) {
        byteNumbers[i] = byteCharacters.charCodeAt(i);
    }
    return new Uint8Array(byteNumbers);
}

let constructSignature = function(timestamp, UID, secretKey) {  
    let baseString = timestamp + "_" + UID;
    let binaryBaseString = ConvertUTF8ToBytes(baseString)
    let binaryKey = ConvertFromBase64ToBytes(secretKey)
    let binarySignature = Crypto.createHmac('sha1', binaryKey)
      .update( binaryBaseString )
      .digest('base64');
    return binarySignature;            
};

Here is a full implementation of your code:

let atob = function(a){
    return new Buffer(a, 'base64').toString('binary');
} 

function ConvertUTF8ToBytes(str) {
    var utf8 = unescape(encodeURIComponent(str));
    var arr = [];
    for (var i = 0; i < utf8.length; i++) {
        arr.push(utf8.charCodeAt(i));
    }
    return new Uint8Array(arr);
}

let ConvertFromBase64ToBytes = function (b64Data){
    var byteCharacters = atob(b64Data);
    var byteNumbers = new Array(byteCharacters.length);
    for (var i = 0; i < byteCharacters.length; i++) {
        byteNumbers[i] = byteCharacters.charCodeAt(i);
    }
    return new Uint8Array(byteNumbers);
}

let constructSignature = function(timestamp, UID, secretKey) {  
    let baseString = timestamp + "_" + UID;
    let binaryBaseString = ConvertUTF8ToBytes(baseString)
    let binaryKey = ConvertFromBase64ToBytes(secretKey)
    let binarySignature = Crypto.createHmac('sha1', binaryKey)
      .update( binaryBaseString )
      .digest('base64');
    return binarySignature;            
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文