Permalink
Browse files

blind alley - for evidence

  • Loading branch information...
1 parent 3053f4d commit 2c57acea6136e0e8ae012087702a3e719c9e9579 @langpavel committed Oct 1, 2012
Showing with 98 additions and 1 deletion.
  1. +36 −1 lib/crypto.js
  2. +62 −0 test/simple/test-crypto-stream.js
View
@@ -19,12 +19,14 @@
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.
+var util = require('util');
+var Stream = require('stream').Stream;
try {
var binding = process.binding('crypto');
var SecureContext = binding.SecureContext;
var Hmac = binding.Hmac;
- var Hash = binding.Hash;
+ var HashBinding = binding.Hash;
var Cipher = binding.Cipher;
var Decipher = binding.Decipher;
var Sign = binding.Sign;
@@ -127,6 +129,39 @@ exports.createCredentials = function(options, context) {
};
+var Hash = function Hash(hash) {
+ this._hash = new HashBinding(hash);
+
+ Stream.call(this);
+
+ this.writable = true;
+
+};
+util.inherits(Hash, Stream);
+
+
+Hash.prototype.write = function write(data, input_encoding) {
+ this._hash.update(data, input_encoding);
+ return true;
+};
+
+
+Hash.prototype.update = function(data, input_encoding) {
+ return this._hash.update(data, input_encoding);
+};
+
+
+Hash.prototype.digest = function(encoding) {
+ return this._hash.digest(encoding);
+};
+
+
+Hash.prototype.end = function(data, input_encoding) {
+ if(typeof data !== 'undefined')
+ this.write(data, input_encoding);
+ this.destroySoon();
+};
+
exports.Hash = Hash;
exports.createHash = function(hash) {
return new Hash(hash);
@@ -0,0 +1,62 @@
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var common = require('../common');
+var assert = require('assert');
+var path = require('path');
+var fs = require('fs');
+
+try {
+ var crypto = require('crypto');
+} catch (e) {
+ console.log('Not compiled with OPENSSL support.');
+ process.exit();
+}
+
+
+var filePath = path.join(common.fixturesDir, 'person.jpg');
+var referenceBuffer = fs.readFileSync(filePath);
+var readStreamOptions = {
+ flags: 'r',
+ bufferSize: 1024
+};
+
+
+var testHash = function(hashName, hashEncoding) {
+ // create reference digest
+ var hashBuffer = crypto.createHash(hashName);
+ hashBuffer.update(referenceBuffer);
+ var digestBuffer = hashBuffer.digest(hashEncoding);
+
+ // create digest from stream
+ var hashStream = crypto.createHash(hashName);
+ var stream = fs.createReadStream(filePath, readStreamOptions);
+ stream.pipe(hashStream);
+ stream.on('end', function() {
+ var digestStream = hashStream.digest(hashEncoding);
+ stream.close();
+
+ assert.deepEqual(digestStream, digestBuffer);
+ });
+};
+
+
+testHash('sha1', 'hex');

0 comments on commit 2c57ace

Please sign in to comment.