Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
// obtain local public / secret keys
// message (publicKey1 + publicKey2 + timestamp) signed by the private key of the user represented by publicKey1
let msg;
if (user === 'userA') {
msg = strToUint8Array(
publicKey.toString() + publicKey2.toString() + timestamp,
);
} else if (user === 'userB') {
msg = strToUint8Array(
publicKey2.toString() + publicKey.toString() + timestamp,
);
}
const msgStr = Buffer.from(msg).toString();
const signedMsg = nacl.sign(msg, secretKey);
dispatch(
setPairingMessage({
msg,
msgStr,
signedMsg,
}),
);
// // testing signed message
// const genKeys = nacl.sign.keyPair();
// // console.warn(publicKey.toString());
// console.log(Buffer.from(message).toString());
// const sig1 = nacl.sign.detached(message, secretKey);
// const sig2 = nacl.sign.detached(message, genKeys.secretKey);
// // console.log(publicKey instanceof Uint8Array);
= ({ cipher, secret }) => {
// convert b64 secret to nonce + key typed arrays
const [nonce, key] = split(NONCE_LEN, decodeBase64(secret))
// convert b64 cipher to typed array "box", then open it
const box = decodeBase64(cipher)
const bytes = secretbox.open(box, nonce, key)
// return the utf8 message
return encodeUTF8(bytes)
}
secretKey: uint8_hex(key.priv),
});
break;
default: throw "Unsupported key type"
}
}
pack = JSON.stringify(pack);
pack = stringToHex(pack);
pack = new Buffer(pack, 'hex');
var context = blake.blake2bInit(32);
blake.blake2bUpdate(context, pack);
var checksum = blake.blake2bFinal(context);
var salt = new Buffer(nacl.randomBytes(16));
var key = pbkdf2.pbkdf2Sync(passPhrase, salt, iterations, 32, 'sha1');
var options = { mode: AES.CBC, padding: Iso10126 };
var encryptedBytes = AES.encrypt(pack, key, salt, options);
var payload = Buffer.concat([new Buffer(checksum), salt, encryptedBytes]);
// decrypt to check if wallet was corrupted during ecryption somehow
if(api.decryptAndCheck(payload).toString('hex') === false)
return api.pack(); // try again, shouldnt happen often
return payload.toString('hex');
}
function delete_private_key (name) {
// FIXME: poor man's secure erase
for (var i = 0; i < 5; ++i) {
seed = nacl.randomBytes(nacl.sign.seedLength);
localStorage.setItem(name, util.btoa(seed));
localStorage.setItem(name, '');
localStorage.setItem(name, null);
}
}
module.exports.encrypt = function (message/* : Uint8Array */,
secretboxKey/* : Uint8Array */, counter/* : number */,
nonceBytes/* : Uint8Array */) {
const nonce = module.exports.getNonce(counter, nonceBytes)
return {
nonce,
ciphertext: nacl.secretbox(message, nonce, secretboxKey)
}
}
function encrypt(key: string, msg: string): string {
const newNonce = () => nacl.randomBytes(nacl.secretbox.nonceLength);
const keyUint8Array = nacl.util.decodeBase64(key);
const nonce = newNonce();
const messageUint8 = nacl.util.decodeUTF8(msg);
const box = nacl.secretbox(messageUint8, nonce, keyUint8Array);
const fullMessage = new Uint8Array(nonce.length + box.length);
fullMessage.set(nonce);
fullMessage.set(box, nonce.length);
// base64 full message;
return nacl.util.encodeBase64(fullMessage);
}
function hmac(data) {
return nacl.hash(Uint8Tool.concat(okeypad, nacl.hash(Uint8Tool.concat(ikeypad, data))));
}
// BIG endian encoding of integer i.
module.exports.sign = function (message/* : Uint8Array */,
secretKey/* : Uint8Array */) {
return nacl.sign(message, secretKey)
}
// @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)
create: async (paperKeys: string, password: string): boolean => {
const keyUnit8 = SHA256(password);
const seedHexUnit8 = TextEncoding.toUnit8(PaperKey.toSeedHex(paperKeys));
const nonce = await randomBytes(secretbox.nonceLength);
const vault = await secretbox(seedHexUnit8, nonce, keyUnit8);
// eslint-disable-next-line no-return-await
return await Vault.save('seed', {
nonce: Base64.encode(nonce),
vault: Base64.encode(vault),
});
},