Add crypto.randomFillSync() and crypto.randomFill()

This commit is contained in:
Flarna
2017-05-09 17:56:56 +02:00
parent 46ad6f9833
commit 97ba49c9d1
2 changed files with 48 additions and 21 deletions

View File

@@ -3478,6 +3478,13 @@ declare module "crypto" {
export function randomBytes(size: number, callback: (err: Error, buf: Buffer) => void): void;
export function pseudoRandomBytes(size: number): Buffer;
export function pseudoRandomBytes(size: number, callback: (err: Error, buf: Buffer) => void): void;
export function randomFillSync(buffer: Buffer | Uint8Array, offset?: number, size?: number): Buffer;
export function randomFill(buffer: Buffer, callback: (err: Error, buf: Buffer) => void): void;
export function randomFill(buffer: Uint8Array, callback: (err: Error, buf: Uint8Array) => void): void;
export function randomFill(buffer: Buffer, offset: number, callback: (err: Error, buf: Buffer) => void): void;
export function randomFill(buffer: Uint8Array, offset: number, callback: (err: Error, buf: Uint8Array) => void): void;
export function randomFill(buffer: Buffer, offset: number, size: number, callback: (err: Error, buf: Buffer) => void): void;
export function randomFill(buffer: Uint8Array, offset: number, size: number, callback: (err: Error, buf: Uint8Array) => void): void;
export interface RsaPublicKey {
key: string;
padding?: number;

View File

@@ -632,10 +632,10 @@ namespace crypto_tests {
}
{
let hmac: crypto.Hmac;
(hmac = crypto.createHmac('md5', 'hello')).end('world', 'utf8', () => {
let hmac: crypto.Hmac;
(hmac = crypto.createHmac('md5', 'hello')).end('world', 'utf8', () => {
let hash: Buffer | string = hmac.read();
});
});
}
{
@@ -644,13 +644,13 @@ namespace crypto_tests {
let clearText: string = "This is the clear text.";
let cipher: crypto.Cipher = crypto.createCipher("aes-128-ecb", key);
let cipherText: string = cipher.update(clearText, "utf8", "hex");
cipherText += cipher.final("hex");
cipherText += cipher.final("hex");
let decipher: crypto.Decipher = crypto.createDecipher("aes-128-ecb", key);
let clearText2: string = decipher.update(cipherText, "hex", "utf8");
clearText2 += decipher.final("utf8");
clearText2 += decipher.final("utf8");
assert.equal(clearText2, clearText);
assert.equal(clearText2, clearText);
}
{
@@ -659,19 +659,19 @@ namespace crypto_tests {
let clearText: Buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4]);
let cipher: crypto.Cipher = crypto.createCipher("aes-128-ecb", key);
let cipherBuffers: Buffer[] = [];
cipherBuffers.push(cipher.update(clearText));
cipherBuffers.push(cipher.final());
cipherBuffers.push(cipher.update(clearText));
cipherBuffers.push(cipher.final());
let cipherText: Buffer = Buffer.concat(cipherBuffers);
let decipher: crypto.Decipher = crypto.createDecipher("aes-128-ecb", key);
let decipherBuffers: Buffer[] = [];
decipherBuffers.push(decipher.update(cipherText));
decipherBuffers.push(decipher.final());
decipherBuffers.push(decipher.update(cipherText));
decipherBuffers.push(decipher.final());
let clearText2: Buffer = Buffer.concat(decipherBuffers);
assert.deepEqual(clearText2, clearText);
assert.deepEqual(clearText2, clearText);
}
{
@@ -682,6 +682,26 @@ namespace crypto_tests {
assert(crypto.timingSafeEqual(buffer1, buffer2))
assert(!crypto.timingSafeEqual(buffer1, buffer3))
}
{
let buffer: Buffer = new Buffer(10);
crypto.randomFillSync(buffer);
crypto.randomFillSync(buffer, 2);
crypto.randomFillSync(buffer, 2, 3);
crypto.randomFill(buffer, (err: Error, buf: Buffer) => void {});
crypto.randomFill(buffer, 2, (err: Error, buf: Buffer) => void {});
crypto.randomFill(buffer, 2, 3, (err: Error, buf: Buffer) => void {});
let arr: Uint8Array = new Uint8Array(10);
crypto.randomFillSync(arr);
crypto.randomFillSync(arr, 2);
crypto.randomFillSync(arr, 2, 3);
crypto.randomFill(arr, (err: Error, buf: Uint8Array) => void {});
crypto.randomFill(arr, 2, (err: Error, buf: Uint8Array) => void {});
crypto.randomFill(arr, 2, 3, (err: Error, buf: Uint8Array) => void {});
}
}
//////////////////////////////////////////////////
@@ -690,17 +710,17 @@ namespace crypto_tests {
namespace tls_tests {
{
var ctx: tls.SecureContext = tls.createSecureContext({
key: "NOT REALLY A KEY",
cert: "SOME CERTIFICATE",
});
var blah = ctx.context;
var ctx: tls.SecureContext = tls.createSecureContext({
key: "NOT REALLY A KEY",
cert: "SOME CERTIFICATE",
});
var blah = ctx.context;
var connOpts: tls.ConnectionOptions = {
host: "127.0.0.1",
port: 55
};
var tlsSocket = tls.connect(connOpts);
var connOpts: tls.ConnectionOptions = {
host: "127.0.0.1",
port: 55
};
var tlsSocket = tls.connect(connOpts);
}
{