UNPKG

tweetnacl-ts

Version:

Port of TweetNaCl cryptographic library to TypeScript (and ES6)

30 lines 1.05 kB
import { ByteArray } from './array'; import { box, box_open, box_keyPair } from './box'; import { blake2b_init, blake2b_update, blake2b_final } from './blake2b'; export function sealedbox(m, pk) { var c = ByteArray(48 /* Overhead */ + m.length); var ek = box_keyPair(); c.set(ek.publicKey); var nonce = nonce_gen(ek.publicKey, pk); var boxed = box(m, nonce, pk, ek.secretKey); c.set(boxed, ek.publicKey.length); // clear secret key for (var i = 0; i < ek.secretKey.length; i++) ek.secretKey[i] = 0; return c; } export function sealedbox_open(c, pk, sk) { if (c.length < 48 /* Overhead */) return; var epk = c.subarray(0, 32 /* PublicKey */); var nonce = nonce_gen(epk, pk); var boxData = c.subarray(32 /* PublicKey */); return box_open(boxData, nonce, epk, sk); } function nonce_gen(pk1, pk2) { var state = blake2b_init(24 /* Nonce */); blake2b_update(state, pk1); blake2b_update(state, pk2); return blake2b_final(state); } //# sourceMappingURL=sealedbox.js.map