chacha-poly-wasm-web
v1.4.1
Published
ChaCha20-Poly1305 encryption for WebAssembly
Downloads
32
Maintainers
Readme
chacha-poly-wasm-web
Usage
import instantiate, { XChaCha20Poly1305 } from 'chacha-poly-wasm-web'
const encrypt = async (secret: Uint8Array, nonce: Uint8Array, data: Uint8Array): Promise<Uint8Array | null> => {
if (typeof WebAssembly !== 'undefined') {
let xchacha20: XChaCha20Poly1305 | null = null
try {
await instantiate()
xchacha20 = new XChaCha20Poly1305(secret, nonce)
return xchacha20.encrypt(data)
} catch (err) {
console.error(err)
} finally {
if (xchacha20) {
xchacha20.free()
}
}
}
return null
}
const decrypt = async (secret: Uint8Array, nonce: Uint8Array, encrypted_data: Uint8Array): Promise<Uint8Array | null> => {
if (typeof WebAssembly !== 'undefined') {
let xchacha20: XChaCha20Poly1305 | null = null
try {
await instantiate()
xchacha20 = new XChaCha20Poly1305(secret, nonce)
return xchacha20.decrypt(encrypted_data)
} catch (err) {
console.error(err)
} finally {
if (xchacha20) {
xchacha20.free()
}
}
}
return null
}