Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// @flow
import { randomBytes, secretbox } from 'tweetnacl'
import { encodeUTF8, decodeUTF8, encodeBase64, decodeBase64 } from 'tweetnacl-util'
const NONCE_LEN = secretbox.nonceLength
const KEY_LEN = secretbox.keyLength
type Utf8 = string
type Base64 = string
const generateCredentials: () => { nonce: Uint8Array, key: Uint8Array }
= () => ({
nonce: randomBytes(NONCE_LEN),
key: randomBytes(KEY_LEN),
})
const join: (Uint8Array, Uint8Array) => Uint8Array
= (a, b) => {
const c = new Uint8Array(a.length + b.length)
c.set(a)
c.set(b, a.length)
return c
export const generateRandomKey = () =>
encodeBase64(randomBytes(secretbox.keyLength));
export const generatePasswordShaKey = (
constructor () {
this.key = randomBytes(secretbox.keyLength)
}