Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
test: Tests for streaming crypto interfaces
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacs committed Dec 6, 2012
1 parent 1137a8a commit a48aa8c
Showing 1 changed file with 54 additions and 2 deletions.
56 changes: 54 additions & 2 deletions test/simple/test-crypto.js
Expand Up @@ -440,6 +440,11 @@ assert.throws(function() {
var s1 = crypto.createSign('RSA-SHA1') var s1 = crypto.createSign('RSA-SHA1')
.update('Test123') .update('Test123')
.sign(keyPem, 'base64'); .sign(keyPem, 'base64');
var s1stream = crypto.createSign('RSA-SHA1');
s1stream.end('Test123');
s1stream = s1stream.sign(keyPem, 'base64');
assert.equal(s1, s1stream, 'Stream produces same output');

var verified = crypto.createVerify('RSA-SHA1') var verified = crypto.createVerify('RSA-SHA1')
.update('Test') .update('Test')
.update('123') .update('123')
Expand All @@ -448,13 +453,25 @@ assert.strictEqual(verified, true, 'sign and verify (base 64)');


var s2 = crypto.createSign('RSA-SHA256') var s2 = crypto.createSign('RSA-SHA256')
.update('Test123') .update('Test123')
.sign(keyPem); // binary .sign(keyPem, 'binary');
var s2stream = crypto.createSign('RSA-SHA256');
s2stream.end('Test123');
s2stream = s2stream.sign(keyPem, 'binary');
assert.equal(s2, s2stream, 'Stream produces same output');

var verified = crypto.createVerify('RSA-SHA256') var verified = crypto.createVerify('RSA-SHA256')
.update('Test') .update('Test')
.update('123') .update('123')
.verify(certPem, s2); // binary .verify(certPem, s2, 'binary');
assert.strictEqual(verified, true, 'sign and verify (binary)'); assert.strictEqual(verified, true, 'sign and verify (binary)');


var verStream = crypto.createVerify('RSA-SHA256');
verStream.write('Tes');
verStream.write('t12');
verStream.end('3');
verified = verStream.verify(certPem, s2, 'binary');
assert.strictEqual(verified, true, 'sign and verify (stream)');

var s3 = crypto.createSign('RSA-SHA1') var s3 = crypto.createSign('RSA-SHA1')
.update('Test123') .update('Test123')
.sign(keyPem, 'buffer'); .sign(keyPem, 'buffer');
Expand All @@ -464,6 +481,13 @@ var verified = crypto.createVerify('RSA-SHA1')
.verify(certPem, s3); .verify(certPem, s3);
assert.strictEqual(verified, true, 'sign and verify (buffer)'); assert.strictEqual(verified, true, 'sign and verify (buffer)');


var verStream = crypto.createVerify('RSA-SHA1');
verStream.write('Tes');
verStream.write('t12');
verStream.end('3');
verified = verStream.verify(certPem, s3);
assert.strictEqual(verified, true, 'sign and verify (stream)');



function testCipher1(key) { function testCipher1(key) {
// Test encryption and decryption // Test encryption and decryption
Expand All @@ -481,6 +505,20 @@ function testCipher1(key) {
txt += decipher.final('utf8'); txt += decipher.final('utf8');


assert.equal(txt, plaintext, 'encryption and decryption'); assert.equal(txt, plaintext, 'encryption and decryption');

// streaming cipher interface
// NB: In real life, it's not guaranteed that you can get all of it
// in a single read() like this. But in this case, we know it's
// quite small, so there's no harm.
var cStream = crypto.createCipher('aes192', key);
cStream.end(plaintext);
ciph = cStream.read();

var dStream = crypto.createDecipher('aes192', key);
dStream.end(ciph);
txt = dStream.read().toString('utf8');

assert.equal(txt, plaintext, 'encryption and decryption with streams');
} }




Expand Down Expand Up @@ -521,6 +559,20 @@ function testCipher3(key, iv) {
txt += decipher.final('utf8'); txt += decipher.final('utf8');


assert.equal(txt, plaintext, 'encryption and decryption with key and iv'); assert.equal(txt, plaintext, 'encryption and decryption with key and iv');

// streaming cipher interface
// NB: In real life, it's not guaranteed that you can get all of it
// in a single read() like this. But in this case, we know it's
// quite small, so there's no harm.
var cStream = crypto.createCipheriv('des-ede3-cbc', key, iv);
cStream.end(plaintext);
ciph = cStream.read();

var dStream = crypto.createDecipheriv('des-ede3-cbc', key, iv);
dStream.end(ciph);
txt = dStream.read().toString('utf8');

assert.equal(txt, plaintext, 'streaming cipher iv');
} }




Expand Down

0 comments on commit a48aa8c

Please sign in to comment.