Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export function decrypt (box: JsonBox, key: Uint8Array): Uint8Array {
// Check JSON:
if (box.encryptionType !== 0) {
throw new Error('Unknown encryption type')
}
const iv = base16.parse(box.iv_hex)
const ciphertext = base64.parse(box.data_base64)
// Decrypt:
const cipher = new AesCbc(key, iv)
const raw = cipher.decrypt(ciphertext)
// Alternative using node.js crypto:
// const decipher = crypto.createDecipheriv('AES-256-CBC', key, iv);
// let x = decipher.update(box.data_base64, 'base64', 'hex')
// x += decipher.final('hex')
// const data = base16.parse(x)
// Calculate field locations:
const headerSize = raw[0]
const dataSize =
(raw[1 + headerSize] << 24) |
(raw[2 + headerSize] << 16) |
async function publicKeyCreate (privateKey: Uint8Array, compressed: boolean) {
const privateKeyHex = base16.stringify(privateKey)
const publicKeyHex: string = await RNFastCrypto.secp256k1EcPubkeyCreate(privateKeyHex, compressed)
const outBuf = base16.parse(publicKeyHex, { out: Buffer.allocUnsafe })
return outBuf
}
async function privateKeyTweakAdd (privateKey: Uint8Array, tweak: Uint8Array) {
const privateKeyHex = base16.stringify(privateKey)
const tweakHex = base16.stringify(tweak)
const privateKeyTweakedHex: string = await RNFastCrypto.secp256k1EcPrivkeyTweakAdd(privateKeyHex, tweakHex)
const outBuf = base16.parse(privateKeyTweakedHex, { out: Buffer.allocUnsafe })
return outBuf
}
files.forEach(file => {
const { SortIndex, Archived, BitcoinSeed, MK, SyncKey } = file.json
const dataKey = base16.parse(MK)
const bitcoinKey = base16.parse(BitcoinSeed)
const syncKey = base16.parse(SyncKey)
const keys = {
bitcoinKey: base64.stringify(bitcoinKey),
dataKey: base64.stringify(dataKey),
format: 'bip32',
syncKey: base64.stringify(syncKey)
}
const keyInfo = makeKeyInfo('wallet:bitcoin', keys, dataKey)
walletInfos.push(keyInfo)
walletStates[keyInfo.id] = {
sortIndex: SortIndex,
archived: Archived,
deleted: false,
hidden: false
files.forEach(file => {
const { SortIndex, Archived, BitcoinSeed, MK, SyncKey } = file.json
const dataKey = base16.parse(MK)
const bitcoinKey = base16.parse(BitcoinSeed)
const syncKey = base16.parse(SyncKey)
const keys = {
bitcoinKey: base64.stringify(bitcoinKey),
dataKey: base64.stringify(dataKey),
format: 'bip32',
syncKey: base64.stringify(syncKey)
}
const keyInfo = makeKeyInfo('wallet:bitcoin', keys, dataKey)
walletInfos.push(keyInfo)
walletStates[keyInfo.id] = {
sortIndex: SortIndex,
archived: Archived,
deleted: false,
hidden: false
}
function timeScrypt(
data: Uint8Array,
snrp: JsonSnrp,
dklen: number = 32
): Promise<{ hash: Uint8Array, time: number }> {
const salt = base16.parse(snrp.salt_hex)
const startTime = getTime()
log(`starting scrypt n=${snrp.n} r=${snrp.r} p=${snrp.p}`)
return io.scrypt(data, salt, snrp.n, snrp.r, snrp.p, dklen).then(hash => {
const time = getTime() - startTime
log(`finished scrypt n=${snrp.n} r=${snrp.r} p=${snrp.p} in ${time}ms`)
return { hash, time }
})
}
files.forEach(file => {
const { SortIndex, Archived, BitcoinSeed, MK, SyncKey } = file.json
const dataKey = base16.parse(MK)
const bitcoinKey = base16.parse(BitcoinSeed)
const syncKey = base16.parse(SyncKey)
const keys = {
bitcoinKey: base64.stringify(bitcoinKey),
dataKey: base64.stringify(dataKey),
format: 'bip32',
syncKey: base64.stringify(syncKey)
}
const keyInfo = makeKeyInfo('wallet:bitcoin', keys, dataKey)
walletInfos.push(keyInfo)
walletStates[keyInfo.id] = {
sortIndex: SortIndex,
archived: Archived,
deleted: false,
hidden: false
}
})
async function publicKeyTweakAdd (publicKey: Uint8Array, tweak: Uint8Array, compressed: boolean) {
const publicKeyHex = base16.stringify(publicKey)
const tweakHex = base16.stringify(tweak)
const publickKeyTweakedHex: string = await RNFastCrypto.secp256k1EcPubkeyTweakAdd(publicKeyHex, tweakHex, compressed)
const outBuf = base16.parse(publickKeyTweakedHex, { out: Buffer.allocUnsafe })
return outBuf
}
async function pbkdf2DeriveAsync(key: Uint8Array, salt: Uint8Array, iter: number, len: number, alg: string) {
if (alg !== 'sha512') {
throw new Error('ErrorUnsupportedPbkdf2Algorithm: ' + alg)
}
const keyHex = base16.stringify(key)
const saltHex = base16.stringify(salt)
const resultHex = await RNFastCrypto.pbkdf2Sha512(keyHex, saltHex, iter, len)
const outBuf = base16.parse(resultHex, { out: Buffer.allocUnsafe })
return outBuf
}
Object.keys(user.repos).map(async syncKey => {
const paths = makeRepoPaths(io, base16.parse(syncKey), new Uint8Array(0))
await saveChanges(paths.dataDisklet, user.repos[syncKey])
await paths.baseDisklet.setText(
'status.json',
JSON.stringify({ lastSync: 1, lastHash: null })
)
})
)