Skip to content
Browse files

createHmacStream + test

  • Loading branch information...
1 parent 3cb6a4d commit 20c5a815e3e884c362b35a7f62343c0be4986b30 @langpavel committed Oct 2, 2012
Showing with 28 additions and 11 deletions.
  1. +8 −4 lib/crypto.js
  2. +20 −7 test/simple/test-crypto-stream.js
View
12 lib/crypto.js
@@ -222,7 +222,7 @@ DigestStream.prototype.reopen = function() {
case 'hmac':
this._impl = exports.createHmac(this.options.algorithm,
- this.options.password);
+ this.options.key);
break;
default:
@@ -277,17 +277,21 @@ exports.createHashStream = function(algorithm, options) {
};
-exports.createHmacStream = function(algorithm, options) {
+exports.createHmacStream = function(algorithm, key, options) {
if (!options) {
if (typeof algorithm === 'object') {
options = algorithm;
} else {
- options = { algorithm: algorithm };
+ options = {
+ algorithm: algorithm,
+ key: key
+ };
}
} else {
options.algorithm = algorithm;
+ options.key = key;
}
- options.type = 'hash';
+ options.type = 'hmac';
return new DigestStream(options);
};
View
27 test/simple/test-crypto-stream.js
@@ -63,19 +63,19 @@ testHash('sha1', 'hex');
testHash('sha1', 'base64');
-var testHmac = function(hashName, hashEncoding) {
+var testHmac = function(hashName, key, hashEncoding) {
// create reference digest
- var hashBuffer = crypto.createHash(hashName);
- hashBuffer.update(referenceBuffer);
- var digestBuffer = hashBuffer.digest(hashEncoding || 'buffer');
+ var hmacBuffer = crypto.createHmac(hashName, key);
+ hmacBuffer.update(referenceBuffer);
+ var digestBuffer = hmacBuffer.digest(hashEncoding || 'buffer');
// create digest from stream
var stream = fs.createReadStream(filePath, readStreamOptions);
- var hashStream = crypto.createHashStream(hashName, { outputEncoding: hashEncoding });
+ var hmacStream = crypto.createHmacStream(hashName, key, { outputEncoding: hashEncoding });
- stream.pipe(hashStream);
+ stream.pipe(hmacStream);
- hashStream.on('data', function(data) {
+ hmacStream.on('data', function(data) {
assert.deepEqual(data, digestBuffer);
});
};
@@ -92,3 +92,16 @@ testHash('sha1', 'base64');
testHash('sha256', null);
testHash('sha256', 'hex');
testHash('sha256', 'base64');
+
+
+testHmac('md5', 'secret', null);
+testHmac('md5', 'secret', 'hex');
+testHmac('md5', 'secret', 'base64');
+
+testHmac('sha1', 'secret', null);
+testHmac('sha1', 'secret', 'hex');
+testHmac('sha1', 'secret', 'base64');
+
+testHmac('sha256', 'secret', null);
+testHmac('sha256', 'secret', 'hex');
+testHmac('sha256', 'secret', 'base64');

0 comments on commit 20c5a81

Please sign in to comment.
Something went wrong with that request. Please try again.