简体   繁体   English

从 Python 到 Javascript 的加密 hmac 转换

[英]Crypto hmac conversion from Python to Javascript

I'm trying to convert the following function from Python to Javascript. Currently concentrating on the generation of the signature.我正在尝试将以下 function 从 Python 转换为 Javascript。目前专注于签名的生成。

def prepare_url(self, segments, params={}):
        if self.token is not None:
            sig = hmac.new(self.token.key.encode(), digestmod=hashlib.sha1)
            sig.update(self.method.lower().encode())
            sig.update(self.service.lower().encode())
            

            ts = self.get_timestamp(self.token.server_offset)
            sig.update(ts.encode())
            params["timestamp"] = ts

            sig.update(self.token.identifier.lower().encode())
 
            params["signature-method"] = "auth"
            params["signature-version"] = self.version
            params["signature"] = base64.standard_b64encode(sig.digest())
 
        self.url = "%s/%s/%d/" % (self.base_path, self.service, self.version)
        self.url += "/".join([urllib.parse.quote(s) for s in segments])
        if params:
            self.url += "?%s" % urllib.parse.urlencode(params)

In Javascript I'm using the crypto library and have the following code:在 Javascript 中,我正在使用加密库并具有以下代码:

import crypto from 'crypto';

const KEY = '5d016f32-452f-4b9b-8e81-641e14d4d98c';
const METHOD = 'get';
const SERVICE = 'data';
const date = (new Date()).toISOString().slice(0, 19);

const encoded = crypto.createHash('sha1')
    .update(KEY)
    .update(METHOD.toLowerCase())
    .update(SERVICE.toLowerCase())
    .update(date)
    .digest('hex');
console.log(encoded);

const baseEncoded = btoa(encoded);
console.log(baseEncoded);

However the end codes are not comparable.但是,结束代码不可比较。

Python generates using the given inputs: b'oV4RJ6pAz+hxZsxeQthx8gZrhAY=' Python 使用给定输入生成: b'oV4RJ6pAz+hxZsxeQthx8gZrhAY='

Javascript generates: YTZjMmIyYjQzOGEwZGUxZTU1YTNjMWVlYjA3MTA3NTFmODc0MDM3ZQ== Javascript 生成: YTZjMmIyYjQzOGEwZGUxZTU1YTNjMWVlYjA3MTA3NTFmODc0MDM3ZQ==

I googled around but could not find what to change to make this work.我四处搜索,但找不到要更改的内容才能使这项工作正常进行。 In python the digest is hashlib.sha1, this is not available in crypto.在 python 中,摘要是 hashlib.sha1,这在加密中不可用。

Any pointers?任何指针?

Below are the codes Python generates at each of the update steps.以下是 Python 在每个更新步骤中生成的代码。

-----> key: b'bnAcIlriz+t4hTQLBrnjI1aeXBI='
-----> method: b'rc1Y6wKZo8pDKHmhjVNDkhcVNKM='
-----> Service: b'/urBh6Yqk6QI39JhYtSMI9P9QS8='
-----> Time: 5d016f32-452f-4b9b-8e81-641e14d4d98c
-----> Identifier: b'oV4RJ6pAz+hxZsxeQthx8gZrhAY='
-----> FINAL: b'oV4RJ6pAz+hxZsxeQthx8gZrhAY='

Solution:解决方案:

const encoded = crypto.createHmac('sha1', key)
    .update(METHOD.toLowerCase())
    .update(SERVICE.toLowerCase())
    .update(date)
    .update(identify)
    .digest('base64');

Based on the feedback from @PresidentJamesK.Polk I was able to figure this out.根据@PresidentJamesK.Polk 的反馈,我能够弄清楚这一点。

const encoded = crypto.createHmac('sha1', key)
    .update(METHOD.toLowerCase())
    .update(SERVICE.toLowerCase())
    .update(date)
    .update(identify)
    .digest('base64');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM