如何从Google Apps脚本的computeHmacSha256Signature方法获取十六进制值? [英] How to get Hex value from computeHmacSha256Signature method of Google Apps Script?

查看:74
本文介绍了如何从Google Apps脚本的computeHmacSha256Signature方法获取十六进制值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在下面阅读了MD5的解决方案,但我不太明白.
从computeDigest(算法,值)字节中获取字符串表示形式[ ]

I read solution for MD5 below, but I could not quite get it.
get back a string representation from computeDigest(algorithm, value) byte[]

我想用HMAC-SHA256哈希创建API签名.

I'd like to create API signature with HMAC-SHA256 hash.

var date = new Date();
var nonce = Math.floor(date.getTime()/1000);
var url = "https://mysweet.com/api/accounts"
var secret = "my_secret";
var signature = Utilities.computeHmacSha256Signature(nonce+url, secret);

,但它返回字节数组[42,-8,-47,-21,..],并且不能直接用作API签名. 有没有一种简单的方法可以从方法中获取十六进制值?还是转化?

but it returns byte array [42, -8, -47, -21, ..], and it cannot be used as API signature directly. Is there a simple way to get a Hex value from the method? or conversion?

推荐答案

我应用了方法

I applied the method you linked to and get:

var sig = signature.reduce(function(str,chr){
  chr = (chr < 0 ? chr + 256 : chr).toString(16);
  return str + (chr.length==1?'0':'') + chr;
},'');;

这是一个测试功能:

function testSig() {
  var date = new Date();
  var message = "Violets are red";
  var secret = "my_secret";
  var signature = Utilities.computeHmacSha256Signature(message, secret);
  var sig = signature.reduce(function(str,chr){
    chr = (chr < 0 ? chr + 256 : chr).toString(16);
    return str + (chr.length==1?'0':'') + chr;
  },'');
  Logger.log(sig); // fe70fa2e74b3ee0d67aa3c1d5c2844e558fea6802e8cfa58e5d4cbdf8bad25fe
  //output from http://www.freeformatter.com/hmac-generator.html#ad-output is:
  //                  fe70fa2e74b3ee0d67aa3c1d5c2844e558fea6802e8cfa58e5d4cbdf8bad25fe
}

这篇关于如何从Google Apps脚本的computeHmacSha256Signature方法获取十六进制值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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