我正在尝试将Node.js脚本转换为Golang。"error: illegal base64 data at input byte 40"但是,我在解码Base64时总是出错。可能有人帮助我,已经调试和阅读了几个小时的文档......需要帮助!
// Node.js exampe (code copied from Facebook Instant game SDK example)
const CryptoJS = require('crypto-js');
var firstpart = signedRequest.split('.')[0];
firstpart = firstpart.replace(/-/g, '+').replace(/_/g, '/');
const signature = CryptoJS.enc.Base64.parse(firstpart).toString(); // <-- fail here
const dataHash = CryptoJS.HmacSHA256(signedRequest.split('.')[1], '<APP_SECRET>').toString();
var isValid = signature === dataHash;
const json = CryptoJS.enc.Base64.parse(signedRequest.split('.')[1]).toString(CryptoJS.enc.Utf8);
const data = JSON.parse(json);
上面的代码是来自 Facebook 的示例代码,下面的代码(下)是我写的。
parts := strings.Split(signedRequest, ".")
firstPart := parts[0]
replaced := strings.Replace(firstPart, "-", "+", -1)
replaced = strings.Replace(replaced, "_", "/", -1)
signatureByte, err := base64.StdEncoding.DecodeString(replaced) // <-- ERROR here
if err != nil {
fmt.Println("error:", err)
return false, err
}
signature := string(signatureByte)
dataHash := createHmacSHA256(parts[1], "<APP_SECRET>") // TODO: not sure, to string or hex string?
isValid := signature == dataHash
if isValid {
return true, nil
}
Go Playground 在这里https://play.golang.org/p/ilSbqamFdV_-
一只萌萌小番薯
相关分类