简体   繁体   English

使用Java签署密钥HMAC SHA1

[英]Sign Key HMAC SHA1 with Javascript

For some reason I am not able to create a signature from a private key in JS. 由于某种原因,我无法从JS中的私钥创建签名。 Using this online help from google: 使用Google的此在线帮助:

https://m4b-url-signer.appspot.com/ https://m4b-url-signer.appspot.com/

URL: https://google.maps.com/maps/api/geocode/json?address=New+York&client=test 网址: https//google.maps.com/maps/api/geocode/json?address = New + York&client = test

Example Key (fake for the purposes of the exercise) Key: QNade5DtdJKKZbidTsrIgONupc4= 示例密钥(出于练习目的而伪造)密钥:QNade5DtdJKKZbidTsrIgONupc4 =

(Result) Signature: XDsiH5JAY7kJLgA1K2PWlhTdO1k= (结果)签名:XDsiH5JAY7kJLgA1K2PWlhTdO1k =

However, my javascript code: 但是,我的JavaScript代码:

var keyString = 'QNade5DtdJKKZbidTsrIgONupc4=';
    console.log(keyString)

var urlString = encodeURIComponent('/maps/api/geocode/json?address=New+York&client=test');
console.log(urlString)

// We need to decode private key to binary
var decoded_key_words = CryptoJS.enc.Utf8.parse(keyString);
var decoded_key = CryptoJS.enc.Base64.stringify(decoded_key_words);

console.log(decoded_key);

var signature = CryptoJS.HmacSHA1(decoded_key,urlString);
console.log(signature);

//  Encode binary signature to base 64
var encoded_signature = CryptoJS.enc.Base64.stringify(signature);
console.log(encoded_signature)

Gives me a signature: 给我签名:

bOenVNeXI6xI1xlSa77oqGKssyY= bOenVNeXI6xI1xlSa77oqGKssyY =

I can't seem to figure out what I'm doing wrong. 我似乎无法弄清楚我在做什么错。 Am I decoding base64 incorrectly? 我是否错误地解码了base64?

For the record, this worked: 为了记录,这有效:

<script src="sha.js"></script>

var url = '/maps/api/geocode/json?address=New+York&client=test';
var key = 'QNade5DtdJKKZbidTsrIgONupc4='

var hmacObj = new jsSHA(url, 'TEXT');
var hmacOutput = hmacObj.getHMAC(key,'B64','SHA-1','B64');

console.log(hmacOutput)

Giving me: 给我:

XDsiH5JAY7kJLgA1K2PWlhTdO1k= XDsiH5JAY7kJLgA1K2PWlhTdO1k =

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

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