wampy
Version:
Amazingly fast, feature-rich, lightweight WAMP Javascript client (for browser and node.js)
44 lines • 1.47 kB
JavaScript
// src/auth/cryptosign/wampy-cryptosign.ts
import tweetnacl from "tweetnacl";
var { sign: NaclSign } = tweetnacl;
function hex2bytes(str) {
return new Uint8Array(str.match(/../g).map((h) => Number.parseInt(h, 16)));
}
function bytes2hex(bytes) {
return bytes ? Array.from(bytes, function(byte) {
return ("0" + (byte & 255).toString(16)).slice(-2);
}).join("") : null;
}
function sign(privateKey) {
const keyPair = privateKey.length === 64 ? NaclSign.keyPair.fromSeed(hex2bytes(privateKey)) : NaclSign.keyPair.fromSecretKey(hex2bytes(privateKey));
return function(method, info) {
if (method === "cryptosign") {
if (!info.challenge) {
throw new Error("No challenge provided!");
}
const l = info.challenge.length;
if (l % 2 !== 0) {
throw new Error("Expected challenge to be an even number of characters!");
}
const signature = NaclSign.detached(hex2bytes(info.challenge), keyPair.secretKey);
return bytes2hex(signature) + info.challenge;
} else {
throw new Error("Unknown authentication method requested!");
}
};
}
var wampy_cryptosign_default = sign;
export {
bytes2hex,
wampy_cryptosign_default as default,
hex2bytes,
sign
};
/**
* Wampy.js Cryptosign-based Authentication plugin
*
* Copyright 2022 KSDaemon. Licensed under the MIT License.
* See @license text at http://www.opensource.org/licenses/mit-license.php
*
*/
//# sourceMappingURL=wampy-cryptosign.js.map