const r = require("sodium-native"),
o = 1,
t = 1
function e(o, t) {
const e = r.sodium_malloc(r.crypto_secretbox_KEYBYTES),
s = r.sodium_malloc(Buffer.byteLength(t, "utf-8")),
c = r.sodium_malloc(Buffer.byteLength(o, "utf-8"))
if ((s.write(t, "utf-8"), c.write(o, "utf-8"), c.byteLength < 32))
throw (r.sodium_memzero(s), r.sodium_memzero(c), new Error("Phrase is too small"))
const a = r.crypto_pwhash_SALTBYTES,
y = r.crypto_secretbox_NONCEBYTES,
u = r.crypto_secretbox_MACBYTES,
p = Buffer.alloc(1 + a + y + c.length + u)
p[0] = 1
const h = p.subarray(1, 1 + a),
n = p.subarray(1 + a, 1 + a + y),
f = p.subarray(1 + a + y)
;(r.randombytes_buf(h), r.randombytes_buf(n))
try {
;(r.crypto_pwhash(
e,
s,
h,
r.crypto_pwhash_OPSLIMIT_INTERACTIVE,
r.crypto_pwhash_MEMLIMIT_INTERACTIVE,
r.crypto_pwhash_ALG_ARGON2ID13,
),
r.crypto_secretbox_easy(f, c, n, e))
} finally {
;(_(e), _(s), _(c))
}
return p
}
function s(o, t) {
const e = r.sodium_malloc(r.crypto_secretbox_KEYBYTES),
s = r.sodium_malloc(Buffer.byteLength(t), "utf-8")
s.write(t, "utf-8")
const c = r.crypto_pwhash_SALTBYTES,
a = r.crypto_secretbox_NONCEBYTES,
y = r.crypto_secretbox_MACBYTES
if (1 !== o[0]) throw new Error("Version mismatch")
const u = o.subarray(1, 1 + c),
p = o.subarray(1 + c, 1 + c + a),
h = o.subarray(1 + c + a),
n = Buffer.alloc(h.length - y)
try {
if (
(r.crypto_pwhash(
e,
s,
u,
r.crypto_pwhash_OPSLIMIT_INTERACTIVE,
r.crypto_pwhash_MEMLIMIT_INTERACTIVE,
r.crypto_pwhash_ALG_ARGON2ID13,
),
!r.crypto_secretbox_open_easy(n, h, p, e))
)
throw new Error("Decryption failed – bad password or corrupted data")
} finally {
;(_(e), _(s))
}
return n
}
function _(o) {
;(r.sodium_memzero(o), r.sodium_free(o))
}
module.exports = { encryptPhrase: e, decryptPhrase: s, free: _ }