编码为base64的Google-App-Script与php [英] Google-App-Script vs php in encoding base64

查看:104
本文介绍了编码为base64的Google-App-Script与php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此php代码在使用SHA 512进行哈希处理之前先对密钥进行解码

This php code decodes the secret key before hashing with SHA 512

$API_SECRET_KEY="W0+m0Dc9GMN9yDVeq3GMDsJ49WasEhQHkNHNuDw3wNg=";
$BDAPI_SECRET_KEY=base64_decode($API_SECRET_KEY);
$HMAC_SIGN = base64_encode(hash_hmac('sha512',$MESSAGE,$BDAPI_SECRET_KEY,true));
echo $HMAC_SIGN;

BfVNi21gY09c8M18cWBRBgo1W9pAlXM99ZVoF7Kz2ETFnIuvXjj8NRvRgn/GaT/m6YJ8efsr5s9EDbIhznAaag==

我想在Google应用脚本中复制它

I want to replicate this in google app script

var Secret = "W0+m0Dc9GMN9yDVeq3GMDsJ49WasEhQHkNHNuDw3wNg="  
var BDSecret= Utilities.base64Decode(Secret)
var hmac = Utilities.base64Encode(Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, message, BDSecret ));
    Logger.log(hmac)

ew5KhLWSJixn8zw4s6VkpYIwvGBjrmjY3LhNWZr9CVEw6W22LOGg+lVzA3uQgOVyICSCffw2bzTepnBdoYtldw==

如果我在散列之前不解码API,它们将返回相同的结果.但是出于此特定目的,需要对密钥进行解码.消息变量只是我的名字"Parit",以防有人要复制.

If I do not decode the API before hashing they return the same result. But for this particular purpose, the key needs to be decoded. The message variable is just my first name "Parit" in case someone wants to replicate.

推荐答案

我认为Utilities.computeHmacSignature()可能无法使用[] byte作为值.因此,作为解决方法,如何使用jsSHA?我认为您可以使用 https://github. com/Caligatio/jsSHA/blob/master/src/sha512.js .

I thought that Utilities.computeHmacSignature() might not be able to use []byte for the value. So as a workaround, how about using jsSHA? I think that in your case, you can use https://github.com/Caligatio/jsSHA/blob/master/src/sha512.js.

使用jsSHA的流程如下.

The flow for using jsSHA is as follows.

  1. 下载sha512.js.
  2. 在脚本编辑器上,创建新脚本,例如,文件名sha512.js.
    • 将sha512.js脚本复制并粘贴到创建的脚本中.
  1. Download sha512.js.
  2. On script editor, create new script as for example, the filename of sha512.js.
    • Copy and paste the script of sha512.js to the created script.

示例脚本:

function myFunction() {
  var message = "Parit";
  var secret = "W0+m0Dc9GMN9yDVeq3GMDsJ49WasEhQHkNHNuDw3wNg=";
  var obj = new jsSHA("SHA-512", "TEXT");
  obj.setHMACKey(secret, "B64");
  obj.update(message);
  Logger.log(obj.getHMAC("B64"))
}

注意:

  • 当我为message测试Parit时,得到了BfVNi21gY09c8M18cWBRBgo1W9pAlXM99ZVoF7Kz2ETFnIuvXjj8NRvRgn/GaT/m6YJ8efsr5s9EDbIhznAaag==.
  • Note :

    • When I tested Parit for message, I got BfVNi21gY09c8M18cWBRBgo1W9pAlXM99ZVoF7Kz2ETFnIuvXjj8NRvRgn/GaT/m6YJ8efsr5s9EDbIhznAaag==.
    • 抱歉,这对您没有用.

      通过Google在2018年6月19日的更新必须能够使用字节数组.这样,仅使用本机Google Apps Scvript,就可以在不使用jsSHA的情况下检索结果.所以我想更新我的答案.

      By the Google's update at June 19, 2018, Utilities.computeHmacSignature() got to be able to use the byte arrays. By this, using only native Google Apps Scvript, the result can be retrieved without using jsSHA. So I would like to update my answer.

      function myFunction() {
        var message = "Parit";
        var secret = "W0+m0Dc9GMN9yDVeq3GMDsJ49WasEhQHkNHNuDw3wNg=";
      
        var value = Utilities.base64Decode(Utilities.base64Encode(message));
        var key = Utilities.base64Decode(secret);
        var out = Utilities.computeHmacSignature(Utilities.MacAlgorithm.HMAC_SHA_512, value, key);
        var res = Utilities.base64Encode(out)
        Logger.log(res)
      }
      

      结果:

      BfVNi21gY09c8M18cWBRBgo1W9pAlXM99ZVoF7Kz2ETFnIuvXjj8NRvRgn/GaT/m6YJ8efsr5s9EDbIhznAaag==
      

      这篇关于编码为base64的Google-App-Script与php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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