javascript - 使用应用脚本在 Google 表格中进行 sha-256 加密
问题描述
我正在尝试将 3 个不同的元素加密为 API 的 http 标头的身份验证令牌。该 API 内置于 Google 表格中,目前我无法使用其他任何东西。
身份验证令牌需要 4 个部分:
- API 密钥
- UTC 格式的时间戳
- API 操作
- API 密钥
API KEY:TIMESTAMP:API ACTION:API Secret Key 格式
出于本示例的目的,假设
- API 密钥为 test123,
- UTC 日期:2011 年 4 月 14 日星期四 22:44:22 GMT
- API 操作是“分类帐”
- API 密钥是 UAV213Q
当我在 python 中使用以下格式“ test123:Thu, 14 Apr 2011 22:44:22 GMT:ledger:UAV213Q ”测试示例时,我得到了结果15594d1f608134cbfa3075ecda4664519cd198738b8f5c3ffa2c95272b854199
这是我使用的python脚本
def sha256():
# tested on Python 3.8.5
from urllib import parse, request
import hashlib
import datetime
from time import strftime, gmtime
# credentials and request params
my_merchant_id = 'apikey'
api_token = 'test123'
api_secret_key = 'UAV213Q'
my_timestamp = strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime())
api_version = 2.9
action_verb = 'ledger'
# set up request params
data = parse.urlencode({'merchantId': my_merchant_id, 'token': api_token,
'version': api_version, 'action': action_verb})
# authentication
sig = api_token + ':' + my_timestamp + ':' + action_verb + ':' + api_secret_key
sig_hash = hashlib.sha256(sig.encode('utf-8')).hexdigest()
my_headers = {'x-ShareASale-Date': my_timestamp,
'x-ShareASale-Authentication': sig_hash}
print(sig_hash)
我尝试使用以下其他 StackOverFlow 问题中的解决方案 如何让 Google Apps 脚本进行 SHA-256 加密?, sha3-256 的 Google 电子表格中的单元格文本,所有建议。
但是,我不断收到错误消息“此函数不允许使用 NOW()、RAND() 或 RANDBETWEEN() 引用单元格。”
我尝试通过在 A1 中使用 NOW() 并使用 B1 =A1 来引用间接引用 NOW() 的单元格,我什至尝试使用 TEXT() 将其转换为文本。
API 密钥需要具有时间戳才能正常工作。我正在考虑在 App 脚本本身中计算它,因为它是一个已知常数。例如,在加密脚本中,它将对 api 令牌进行硬编码,以 utc 格式调用时间戳,并以正确的格式对 api 密钥进行硬编码,并且可能只是添加操作的函数,以便我可以进行更改,因此它将是 sha256 (分类帐)并将其合并到加密中
解决方案
这个答案怎么样?
修改点:
- 当我看到您的 python 脚本时,我确认您的问题中显示的规范与 python 脚本的规范不同。
- 似乎
Thu, 14 Apr 2011 22:44:22 GMT
是Thu, 14 Apr 2011 22:44:22 +0000
- 似乎需要使用摘要的“SHA_256”。
- 似乎
示例脚本:
当您的 python 脚本转换为 Google Apps 脚本时,它变为如下。请将其复制并粘贴到脚本编辑器并在脚本编辑器中运行该功能myFunction
。这样,您可以在日志中看到结果值。
function myFunction() {
const api_token = 'test123';
const api_secret_key = 'UAV213Q';
const my_timestamp = 'Thu, 14 Apr 2011 22:44:22 +0000';
const action_verb = 'ledger';
const value = `${api_token}:${my_timestamp}:${action_verb}:${api_secret_key}`;
const bytes = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, value);
const res = bytes.map(byte => ('0' + (byte & 0xFF).toString(16)).slice(-2)).join('');
console.log(res)
}
结果:
当test123:Thu, 14 Apr 2011 22:44:22 +0000:ledger:UAV213Q
用于上述 Google Apps Script 和您的 python 脚本时,两者的结果相同如下。
8c3a6873fe71c402dc1e3ca7bc828712e3dfb7a66ed09feeeca2152dd809df81
参考:
添加:
回答附加问题 1:
当您想检索日期字符串Thu, 14 Apr 2011 22:44:22 +0000
时,请使用以下脚本。
const my_timestamp = new Date().toUTCString().replace("GMT", "+0000");
回答附加问题 2:
当您想检索大写的值时,请使用以下脚本。但是当我测试你的python脚本时,结果值是小写的。所以请注意这一点。
function myFunction() {
const api_token = 'test123';
const api_secret_key = 'UAV213Q';
const my_timestamp = 'Thu, 14 Apr 2011 22:44:22 +0000';
const action_verb = 'ledger';
const value = `${api_token}:${my_timestamp}:${action_verb}:${api_secret_key}`;
const bytes = Utilities.computeDigest(Utilities.DigestAlgorithm.SHA_256, value);
const res = bytes.map(byte => ('0' + (byte & 0xFF).toString(16)).slice(-2)).join('').toUpperCase();
console.log(res)
}
- 在这种情况下,
8C3A6873FE71C402DC1E3CA7BC828712E3DFB7A66ED09FEEECA2152DD809DF81
得到。
参考:
推荐阅读
- azure - 特定 IP 的 Azure 应用程序网关重定向排除?
- c# - EntityFrameworkCore 交换 DbConnection(添加事务)
- firebase - 任务“:assets_audio_player:compileDebugKotlin”执行失败
- javascript - 过滤数组并在之后创建新数组的函数
- .net - “CreateAppHost”任务意外失败。#15495
- python - Count duplicates in dictionary by specific keys
- amazon-web-services - 在 2 个或更多实例之间添加“冷却”或“暂停”时间段,同时减少所需容量
- javascript - Ipad 上 pageY 的值冲突
- javascript - chart.js 图表无法呈现
- go - 从 DDD 的角度来看,如何处理 DB 特定类型?