Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
bigInt.rand = function (bitLength) {
let bytes = bitLength / 8;
let buf = Buffer.alloc(bytes);
crypto.randomFillSync(buf);
buf[0] = buf[0] | 128; // first bit to 1 -> to get the necessary bitLength
return bigInt.fromArray([...buf], 256);
};
function fetchAndDecipher(url, key, iv, authTag, progressEventName) {
if (typeof key === 'string')
key = Buffer.from(key, 'base64');
if (typeof iv === 'string')
iv = Buffer.from(iv, 'base64');
if (typeof authTag === 'string')
authTag = Buffer.from(authTag, 'base64');
var decipher = crypto_browserify_1.createDecipheriv('aes-256-gcm', key, iv);
decipher.setAuthTag(authTag);
return (fetch(url)
// Retrieve its body as ReadableStream
.then(function (response) {
if (response.body === null)
throw new Error('Response body is empty!');
var contentLength = response.headers.get('Content-Length');
return decryptStream(response.body, decipher, Number(contentLength), url, progressEventName);
}));
}
/**
function fetchAndDecipher(
url: string,
key: string | Buffer,
iv: string | Buffer,
authTag: string | Buffer,
): Promise {
if (typeof key === 'string') key = Buffer.from(key, 'base64');
if (typeof iv === 'string') iv = Buffer.from(iv, 'base64');
if (typeof authTag === 'string') authTag = Buffer.from(authTag, 'base64');
const decipher = createDecipheriv(
'aes-256-gcm',
key,
iv,
);
decipher.setAuthTag(authTag);
return (
fetch(url)
// Retrieve its body as ReadableStream
.then(response => {
if (response.body === null) throw new Error('Response body is empty!');
const contentLength = response.headers.get('Content-Length');
return decryptStream(
response.body,
// If the file is unencrypted, simply return the readable stream
if (!decryptionOptions) {
return response.body;
}
// Else we need to decrypt the blob
const { iv, authTag, key } = decryptionOptions;
// Convert to buffers
const bufferKey = toBuff(key);
// Grab from header if possible
const bufferIv = toBuff(response.headers.get('x-penumbra-iv') || iv);
const bufferAuthTag = toBuff(authTag);
// Construct the decipher
const decipher = createDecipheriv('aes-256-gcm', bufferKey, bufferIv);
decipher.setAuthTag(bufferAuthTag);
// Decrypt the stream
return decryptStream(
response.body,
decipher,
Number(response.headers.get('Content-Length') || '0'),
url,
);
})
);
export function fetchAndDecipher(url, key, iv, authTag) {
const decipher = createDecipheriv(
'aes-256-gcm',
Buffer.from(key, 'base64'),
Buffer.from(iv, 'base64')
);
decipher.setAuthTag(Buffer.from(authTag, 'base64'));
return (
fetch(url)
// Retrieve its body as ReadableStream
.then(response => {
const contentLength = response.headers.get('Content-Length');
return decryptStream(
response.body,
decipher,
Number(contentLength),
const { id } = file;
// eslint-disable-next-line no-param-reassign
size = file.size || size;
// Convert to Buffers
const key = options.key instanceof Buffer ? options.key : toBuff(options.key);
const iv =
options.iv instanceof Buffer ? options.iv : Buffer.from(options.iv);
const authTag =
options.authTag instanceof Buffer
? options.authTag
: toBuff(options.authTag);
// Construct the decipher
const decipher = createDecipheriv('aes-256-gcm', key, iv);
decipher.setAuthTag(authTag);
// Encrypt the stream
return {
...file,
// stream:
// file.stream instanceof ReadableStream
// ? encryptStream(file.stream, cipher, size)
// : encryptBuffer(file.stream, cipher),
stream: decryptStream(
file.stream instanceof ReadableStream
? file.stream
: ((intoStream(file.stream) as unknown) as ReadableStream),
/** TODO: address this TypeScript confusion */
decipher,
size,
key: Buffer.from(
crypto.getRandomValues(new Uint8Array(GENERATED_KEY_RANDOMNESS / 8)),
),
};
}
const { id } = file;
// eslint-disable-next-line no-param-reassign
size = file.size || size;
// Convert to Buffers
const key = toBuff(options.key);
const iv = Buffer.from(crypto.getRandomValues(new Uint8Array(IV_RANDOMNESS)));
// Construct the decipher
const cipher = createCipheriv('aes-256-gcm', key, iv);
// Encrypt the stream
return {
...file,
// stream:
// file.stream instanceof ReadableStream
// ? encryptStream(file.stream, cipher, size)
// : encryptBuffer(file.stream, cipher),
stream: encryptStream(
id,
file.stream instanceof ReadableStream
? file.stream
: intoStream(file.stream),
cipher,
size,
key,
iv,
/* eslint-disable */
const crypto = require('crypto-browserify');
if (typeof navigator != 'undefined' && navigator.product == 'ReactNative') {
// react-native
crypto.getRandomValues = (buffer) => {
for (let round = 0; round < 20; round++) {
for (let i = 0; i < buffer.length; i++) {
if (round) {
buffer[i] ^= Math.trunc(256 * Math.random());
} else {
buffer[i] = Math.trunc(256 * Math.random());
}
}
}
return buffer;
};
crypto.randomBytes = (length) => {
if (length <= 0 || length > 1024 || parseInt(String(length), 10) !== length) {
throw new Error('invalid length');
}
const result = Buffer.from(new Uint8Array(length));
this.GenerateSecret = function () {
var RIPEMD160 = require('ripemd160');
var crypto = require('crypto-browserify');
var secretBuffer = crypto.randomBytes(32);
var secret = secretBuffer.toString('hex');
var hashedSecret = new RIPEMD160().update(secretBuffer).digest('hex');
console.log("\nSecret:\t\t\t", secret);
console.log("\Hashed Secret:\t\t", hashedSecret, "\n");
return { "secret": secret, "hashedSecret": hashedSecret };
};
function iter(block) {
let hash = createHash(opts.digest || 'md5');
hash.update(block);
hash.update(data);
hash.update(salt);
block = hash.digest();
for (let e = 1; e < (opts.count || 1); e++) {
hash = createHash(opts.digest || 'md5');
hash.update(block);
block = hash.digest();
}
return block;
}