Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function makeIo(nativeIo: EdgeNativeIo): EdgeIo {
const clientIo: ClientIo = nativeIo['edge-core']
const { console, disklet, entropy, scrypt } = clientIo
const csprng = new HmacDRBG({
hash: hashjs.sha256,
entropy: base64.parse(entropy)
})
return {
console,
disklet,
random: bytes => csprng.generate(bytes),
scrypt,
// Networking:
fetch(uri: string, opts?: EdgeFetchOptions): Promise {
return window.fetch(uri, opts)
},
const getDRBG = (msg: Buffer) => {
const entropy = randomBytes(ENT_LEN);
const pers = Buffer.allocUnsafe(ALG_LEN + ENT_LEN);
Buffer.from(randomBytes(ENT_LEN)).copy(pers, 0);
ALG.copy(pers, ENT_LEN);
return new DRBG({
hash: hashjs.sha256,
entropy,
nonce: msg,
pers,
});
};
const createForgePrng = (seed) => {
const hmacDrgb = new HmacDrgb({
hash: hash.sha256,
entropy: util.binary.hex.encode(seed),
nonce: null,
pers: null,
});
return {
getBytesSync: (size) => {
const bytesArray = hmacDrgb.generate(size);
const bytes = new Uint8Array(bytesArray);
return util.binary.raw.encode(bytes);
},
};
};
genPriKey: () => {
let key = new Uint8Array(32)
if (window.crypto) {
window.crypto.getRandomValues(key)
key = key.reduce((prev, curr) => {
let hex = ("0" + curr.toString(16)).slice(-2)
return prev + hex
}, "")
} else {
let enc = ""
for (let i = 0; i < 32; i++) {
key[i] = Math.floor(Math.random() * 256)
enc += key[i].toString(16)
}
let d = new hmacDRBG({
hash: hash.sha256,
entropy: enc,
nonce: enc
})
key = d.generate(32, 'hex')
}
return key.toString('hex')
}
}