import { scalarMult } from "@stablelib/x25519" ; import { hsalsa } from "@stablelib/xsalsa20" ; import { secretBox , openSecretBox } from "./secretbox" ; import { wipe } from "@stablelib/wipe" ; export { generateKeyPair } from "@stablelib/x25519" ; const zeros16 = new Uint8Array ( 16 ) ; export function precomputeSharedKey ( theirPublicKey , mySecretKey ) { const key = scalarMult ( mySecretKey , theirPublicKey ) ; hsalsa ( key , zeros16 , key ) ; return key ; } export function box ( theirPublicKey , mySecretKey , nonce , data ) { const sharedKey = precomputeSharedKey ( theirPublicKey , mySecretKey ) ; const result = secretBox ( sharedKey , nonce , data ) ; wipe ( sharedKey ) ; return result ; } export function openBox ( theirPublicKey , mySecretKey , nonce , data ) { const sharedKey = precomputeSharedKey ( theirPublicKey , mySecretKey ) ; const result = openSecretBox ( sharedKey , nonce , data ) ; wipe ( sharedKey ) ; return result ; }