Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: cleaning up test/parallel/test-crypto-binary-default.js #19054

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 20 additions & 5 deletions test/parallel/test-crypto-binary-default.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,16 @@ assert.throws(function() {
}, /^Error: not enough data$/);

// Test HMAC
{
const hmacHash = crypto.createHmac('sha1', 'Node')
.update('some data')
.update('to hmac')
.digest('hex');
assert.strictEqual(hmacHash, '19fd6e1ba73d9ed2224dd5094a71babe85d9a892');
}

// Test HMAC-SHA-* (rfc 4231 Test Cases)
{
const rfc4231 = [
{
key: Buffer.from('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex'),
Expand Down Expand Up @@ -143,7 +146,6 @@ const rfc4231 = [
'e2adebeb10a298dd'
}
},

{
key: Buffer.from('0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c', 'hex'),
// 'Test With Truncation'
Expand Down Expand Up @@ -228,8 +230,10 @@ for (const testCase of rfc4231) {
);
}
}
}

// Test HMAC-MD5/SHA1 (rfc 2202 Test Cases)
{
const rfc2202_md5 = [
{
key: Buffer.from('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex'),
Expand Down Expand Up @@ -356,8 +360,10 @@ for (const testCase of rfc2202_sha1) {
.digest('hex')
);
}
}

// Test hashing
{
const a1 = crypto.createHash('sha1').update('Test123').digest('hex');
const a2 = crypto.createHash('sha256').update('Test123').digest('base64');
const a3 = crypto.createHash('sha512').update('Test123').digest(); // binary
Expand All @@ -375,27 +381,32 @@ assert.strictEqual(a1, '8308651804facb7b9af8ffc53a33a22d6a1c8ac2');

assert.strictEqual(a2, '2bX1jws4GYKTlxhloUB09Z66PoJZW+y+hq5R8dnx9l4=');

// Test SHA512 as assumed latin1
assert.strictEqual(
a3,
'\u00c1(4\u00f1\u0003\u001fd\u0097!O\'\u00d4C/&Qz\u00d4' +
'\u0094\u0015l\u00b8\u008dQ+\u00db\u001d\u00c4\u00b5}\u00b2' +
'\u00d6\u0092\u00a3\u00df\u00a2i\u00a1\u009b\n\n*\u000f' +
'\u00d7\u00d6\u00a2\u00a8\u0085\u00e3<\u0083\u009c\u0093' +
'\u00c2\u0006\u00da0\u00a1\u00879(G\u00ed\'',
'Test SHA512 as assumed latin1'
'\u00c2\u0006\u00da0\u00a1\u00879(G\u00ed\''
);

assert.deepStrictEqual(
a4,
Buffer.from('8308651804facb7b9af8ffc53a33a22d6a1c8ac2', 'hex')
);
}

// Test multiple updates to same hash
{
const h1 = crypto.createHash('sha1').update('Test123').digest('hex');
const h2 = crypto.createHash('sha1').update('Test').update('123').digest('hex');
const h2 = crypto.createHash('sha1').update('Test').update('123')
.digest('hex');
assert.strictEqual(h1, h2);
}

// Test hashing for binary files
{
const fn = fixtures.path('sample.png');
const sha1Hash = crypto.createHash('sha1');
const fileStream = fs.createReadStream(fn);
Expand All @@ -408,6 +419,7 @@ fileStream.on('close', common.mustCall(function() {
'22723e553129a336ad96e10f6aecdf0f45e4149e'
);
}));
}

// Unknown digest method should throw an error:
// https://github.com/nodejs/node-v0.x-archive/issues/2227
Expand All @@ -416,6 +428,7 @@ assert.throws(function() {
}, /^Error: Digest method not supported$/);

// Test signing and verifying
{
const s1 = crypto.createSign('SHA1')
.update('Test123')
.sign(keyPem, 'base64');
Expand All @@ -442,6 +455,7 @@ const s3Verified = crypto.createVerify('SHA1')
.update('123')
.verify(certPem, s3);
assert.strictEqual(s3Verified, true);
}


function testCipher1(key) {
Expand Down Expand Up @@ -567,6 +581,7 @@ common.expectsError(

// Test Diffie-Hellman with two parties sharing a secret,
// using various encodings as we go along
{
const dh1 = crypto.createDiffieHellman(common.hasFipsCrypto ? 1024 : 256);
const p1 = dh1.getPrime('buffer');
const dh2 = crypto.createDiffieHellman(p1, 'base64');
Expand Down Expand Up @@ -620,7 +635,7 @@ assert.strictEqual(

rsaVerify.update(rsaPubPem);
assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true);

}

//
// Test RSA signing and verification
Expand Down