我被困在這幾個星期,以及何時使用出口。結果NodeJS並不是用於解析雲代碼簽名簽名/驗證的最佳選擇(但無論如何),但我確實找到了解決方案。
根據Chris Stewart的回覆here,您可以改爲使用jsrsasign庫。
這裏有一個工作示例的一些雲代碼:
Parse.Cloud.define("verifySignature", function(request, response) {
var KJUR = require('cloud/jsrsasign/npm/lib/jsrsasign.js');
var strPubKey = '-----BEGIN PUBLIC KEY-----(snip)';
// initialize
var pubKey = KJUR.KEYUTIL.getKey(strPubKey);
var data = request.params.data;
var signature = request.params.signature;
var verifier = new KJUR.Signature({"alg": "SHA1withRSA", "prov": "cryptojs/jsrsa"});
// initialize for signature validation
verifier.initVerifyByPublicKey(pubKey);
// update data
verifier.updateString(data);
// verify signature
if (verifier.verify(base64ToHex(signature))
response.success("Signature verified");
else
response.error("Signature could not be verified");
}
function base64ToHex(str) {
for (var i = 0, bin = atob(str.replace(/[ \r\n]+$/, "")), hex = []; i < bin.length; ++i) {
var tmp = bin.charCodeAt(i).toString(16);
if (tmp.length === 1) tmp = "0" + tmp;
hex[hex.length] = tmp;
}
return hex.join(" ");
}
function atob(base64) {
var tableStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/";
var table = tableStr.split("");
if (/(=[^=]+|={3,})$/.test(base64)) throw new Error("String contains an invalid character");
base64 = base64.replace(/=/g, "");
var n = base64.length & 3;
if (n === 1) throw new Error("String contains an invalid character");
for (var i = 0, j = 0, len = base64.length/4, bin = []; i < len; ++i) {
var a = tableStr.indexOf(base64[j++] || "A"), b = tableStr.indexOf(base64[j++] || "A");
var c = tableStr.indexOf(base64[j++] || "A"), d = tableStr.indexOf(base64[j++] || "A");
if ((a | b | c | d) < 0) throw new Error("String contains an invalid character");
bin[bin.length] = ((a << 2) | (b >> 4)) & 255;
bin[bin.length] = ((b << 4) | (c >> 2)) & 255;
bin[bin.length] = ((c << 6) | d) & 255;
};
return String.fromCharCode.apply(null, bin).substr(0, bin.length + n - 4);
}
來源
2014-09-16 15:28:49
Zac
如果'cloud'相對於腳本的目錄,你有沒有嘗試'要求(」 ./雲/ ..);'? – mscdex
引用的js文件位置成功,沒有路徑問題。 –