[node] [crypto] Added missing crypto update types

* Added DataView as type of crypto update functions
This commit is contained in:
Gustaf Räntilä
2017-10-13 14:23:39 +02:00
parent dfdf9e29ec
commit 4bed8394f2
2 changed files with 64 additions and 12 deletions
+12 -12
View File
@@ -4876,23 +4876,23 @@ declare module "crypto" {
type ECDHKeyFormat = "compressed" | "uncompressed" | "hybrid";
export interface Hash extends NodeJS.ReadWriteStream {
update(data: string | Buffer): Hash;
update(data: string | Buffer, input_encoding: Utf8AsciiLatin1Encoding): Hash;
update(data: string | Buffer | DataView): Hash;
update(data: string | Buffer | DataView, input_encoding: Utf8AsciiLatin1Encoding): Hash;
digest(): Buffer;
digest(encoding: HexBase64Latin1Encoding): string;
}
export interface Hmac extends NodeJS.ReadWriteStream {
update(data: string | Buffer): Hmac;
update(data: string | Buffer, input_encoding: Utf8AsciiLatin1Encoding): Hmac;
update(data: string | Buffer | DataView): Hmac;
update(data: string | Buffer | DataView, input_encoding: Utf8AsciiLatin1Encoding): Hmac;
digest(): Buffer;
digest(encoding: HexBase64Latin1Encoding): string;
}
export function createCipher(algorithm: string, password: any): Cipher;
export function createCipheriv(algorithm: string, key: any, iv: any): Cipher;
export interface Cipher extends NodeJS.ReadWriteStream {
update(data: Buffer): Buffer;
update(data: Buffer | DataView): Buffer;
update(data: string, input_encoding: Utf8AsciiBinaryEncoding): Buffer;
update(data: Buffer, input_encoding: any, output_encoding: HexBase64BinaryEncoding): string;
update(data: Buffer | DataView, input_encoding: any, output_encoding: HexBase64BinaryEncoding): string;
update(data: string, input_encoding: Utf8AsciiBinaryEncoding, output_encoding: HexBase64BinaryEncoding): string;
final(): Buffer;
final(output_encoding: string): string;
@@ -4903,9 +4903,9 @@ declare module "crypto" {
export function createDecipher(algorithm: string, password: any): Decipher;
export function createDecipheriv(algorithm: string, key: any, iv: any): Decipher;
export interface Decipher extends NodeJS.ReadWriteStream {
update(data: Buffer): Buffer;
update(data: Buffer | DataView): Buffer;
update(data: string, input_encoding: HexBase64BinaryEncoding): Buffer;
update(data: Buffer, input_encoding: any, output_encoding: Utf8AsciiBinaryEncoding): string;
update(data: Buffer | DataView, input_encoding: any, output_encoding: Utf8AsciiBinaryEncoding): string;
update(data: string, input_encoding: HexBase64BinaryEncoding, output_encoding: Utf8AsciiBinaryEncoding): string;
final(): Buffer;
final(output_encoding: string): string;
@@ -4915,15 +4915,15 @@ declare module "crypto" {
}
export function createSign(algorithm: string): Signer;
export interface Signer extends NodeJS.WritableStream {
update(data: string | Buffer): Signer;
update(data: string | Buffer, input_encoding: Utf8AsciiLatin1Encoding): Signer;
update(data: string | Buffer | DataView): Signer;
update(data: string | Buffer | DataView, input_encoding: Utf8AsciiLatin1Encoding): Signer;
sign(private_key: string | { key: string; passphrase: string }): Buffer;
sign(private_key: string | { key: string; passphrase: string }, output_format: HexBase64Latin1Encoding): string;
}
export function createVerify(algorith: string): Verify;
export interface Verify extends NodeJS.WritableStream {
update(data: string | Buffer): Verify;
update(data: string | Buffer, input_encoding: Utf8AsciiLatin1Encoding): Verify;
update(data: string | Buffer | DataView): Verify;
update(data: string | Buffer | DataView, input_encoding: Utf8AsciiLatin1Encoding): Verify;
verify(object: string | Object, signature: Buffer | DataView): boolean;
verify(object: string | Object, signature: string, signature_format: HexBase64Latin1Encoding): boolean;
// https://nodejs.org/api/crypto.html#crypto_verifier_verify_object_signature_signature_format
+52
View File
@@ -857,9 +857,39 @@ function simplified_stream_ctor_test() {
namespace crypto_tests {
{
// crypto_hash_string_test
var hashResult: string = crypto.createHash('md5').update('world').digest('hex');
}
{
// crypto_hash_buffer_test
var hashResult: string = crypto.createHash('md5')
.update(new Buffer('world')).digest('hex');
}
{
// crypto_hash_dataview_test
var hashResult: string = crypto.createHash('md5')
.update(new DataView(new Buffer('world').buffer)).digest('hex');
}
{
// crypto_hmac_string_test
var hmacResult: string = crypto.createHmac('md5', 'hello').update('world').digest('hex');
}
{
// crypto_hmac_buffer_test
var hmacResult: string = crypto.createHmac('md5', 'hello')
.update(new Buffer('world')).digest('hex');
}
{
// crypto_hmac_dataview_test
var hmacResult: string = crypto.createHmac('md5', 'hello')
.update(new DataView(new Buffer('world').buffer)).digest('hex');
}
{
let hmac: crypto.Hmac;
(hmac = crypto.createHmac('md5', 'hello')).end('world', 'utf8', () => {
@@ -903,6 +933,28 @@ namespace crypto_tests {
assert.deepEqual(clearText2, clearText);
}
{
// crypto_cipher_decipher_dataview_test
let key: Buffer = new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 1, 2, 3, 4, 5, 6, 7]);
let clearText: DataView = new DataView(
new Buffer([1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4]).buffer);
let cipher: crypto.Cipher = crypto.createCipher("aes-128-ecb", key);
let cipherBuffers: Buffer[] = [];
cipherBuffers.push(cipher.update(clearText));
cipherBuffers.push(cipher.final());
let cipherText: DataView = new DataView(Buffer.concat(cipherBuffers).buffer);
let decipher: crypto.Decipher = crypto.createDecipher("aes-128-ecb", key);
let decipherBuffers: Buffer[] = [];
decipherBuffers.push(decipher.update(cipherText));
decipherBuffers.push(decipher.final());
let clearText2: Buffer = Buffer.concat(decipherBuffers);
assert.deepEqual(clearText2, clearText);
}
{
let buffer1: Buffer = new Buffer([1, 2, 3, 4, 5]);
let buffer2: Buffer = new Buffer([1, 2, 3, 4, 5]);