Files

127 lines
4.9 KiB
JavaScript

"use strict";
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// nip44.ts
var nip44_exports = {};
__export(nip44_exports, {
decrypt: () => decrypt,
encrypt: () => encrypt,
utils: () => utils
});
module.exports = __toCommonJS(nip44_exports);
var import_chacha = require("@noble/ciphers/chacha");
var import_utils = require("@noble/ciphers/utils");
var import_secp256k1 = require("@noble/curves/secp256k1");
var import_hkdf = require("@noble/hashes/hkdf");
var import_hmac = require("@noble/hashes/hmac");
var import_sha256 = require("@noble/hashes/sha256");
var import_utils2 = require("@noble/hashes/utils");
var import_base = require("@scure/base");
// utils.ts
var utf8Decoder = new TextDecoder("utf-8");
var utf8Encoder = new TextEncoder();
// nip44.ts
var utils = {
v2: {
maxPlaintextSize: 65536 - 128,
minCiphertextSize: 100,
maxCiphertextSize: 102400,
getConversationKey(privkeyA, pubkeyB) {
const key = import_secp256k1.secp256k1.getSharedSecret(privkeyA, "02" + pubkeyB);
return key.subarray(1, 33);
},
getMessageKeys(conversationKey, salt) {
const keys = (0, import_hkdf.hkdf)(import_sha256.sha256, conversationKey, salt, "nip44-v2", 76);
return {
encryption: keys.subarray(0, 32),
nonce: keys.subarray(32, 44),
auth: keys.subarray(44, 76)
};
},
calcPadding(len) {
if (!Number.isSafeInteger(len) || len < 0)
throw new Error("expected positive integer");
if (len <= 32)
return 32;
const nextpower = 1 << Math.floor(Math.log2(len - 1)) + 1;
const chunk = nextpower <= 256 ? 32 : nextpower / 8;
return chunk * (Math.floor((len - 1) / chunk) + 1);
},
pad(unpadded) {
const unpaddedB = utf8Encoder.encode(unpadded);
const len = unpaddedB.length;
if (len < 1 || len >= utils.v2.maxPlaintextSize)
throw new Error("invalid plaintext length: must be between 1b and 64KB");
const paddedLen = utils.v2.calcPadding(len);
const zeros = new Uint8Array(paddedLen - len);
const lenBuf = new Uint8Array(2);
new DataView(lenBuf.buffer).setUint16(0, len);
return (0, import_utils2.concatBytes)(lenBuf, unpaddedB, zeros);
},
unpad(padded) {
const unpaddedLen = new DataView(padded.buffer).getUint16(0);
const unpadded = padded.subarray(2, 2 + unpaddedLen);
if (unpaddedLen === 0 || unpadded.length !== unpaddedLen || padded.length !== 2 + utils.v2.calcPadding(unpaddedLen))
throw new Error("invalid padding");
return utf8Decoder.decode(unpadded);
}
}
};
function encrypt(key, plaintext, options = {}) {
const version = options.version ?? 2;
if (version !== 2)
throw new Error("unknown encryption version " + version);
const salt = options.salt ?? (0, import_utils2.randomBytes)(32);
(0, import_utils.ensureBytes)(salt, 32);
const keys = utils.v2.getMessageKeys(key, salt);
const padded = utils.v2.pad(plaintext);
const ciphertext = (0, import_chacha.chacha20)(keys.encryption, keys.nonce, padded);
const mac = (0, import_hmac.hmac)(import_sha256.sha256, keys.auth, ciphertext);
return import_base.base64.encode((0, import_utils2.concatBytes)(new Uint8Array([version]), salt, ciphertext, mac));
}
function decrypt(key, ciphertext) {
const u = utils.v2;
(0, import_utils.ensureBytes)(key, 32);
const clen = ciphertext.length;
if (clen < u.minCiphertextSize || clen >= u.maxCiphertextSize)
throw new Error("invalid ciphertext length: " + clen);
if (ciphertext[0] === "#")
throw new Error("unknown encryption version");
let data;
try {
data = import_base.base64.decode(ciphertext);
} catch (error) {
throw new Error("invalid base64: " + error.message);
}
const vers = data.subarray(0, 1)[0];
if (vers !== 2)
throw new Error("unknown encryption version " + vers);
const salt = data.subarray(1, 33);
const ciphertext_ = data.subarray(33, -32);
const mac = data.subarray(-32);
const keys = u.getMessageKeys(key, salt);
const calculatedMac = (0, import_hmac.hmac)(import_sha256.sha256, keys.auth, ciphertext_);
if (!(0, import_utils.equalBytes)(calculatedMac, mac))
throw new Error("invalid MAC");
const padded = (0, import_chacha.chacha20)(keys.encryption, keys.nonce, ciphertext_);
return u.unpad(padded);
}