Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

blind alley - for evidence

  • Loading branch information...
commit 2c57acea6136e0e8ae012087702a3e719c9e9579 1 parent 3053f4d
Pavel Lang authored

Showing 2 changed files with 98 additions and 1 deletion. Show diff stats Hide diff stats

  1. +36 1 lib/crypto.js
  2. +62 0 test/simple/test-crypto-stream.js
37 lib/crypto.js
@@ -19,12 +19,14 @@
19 19 // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20 20 // USE OR OTHER DEALINGS IN THE SOFTWARE.
21 21
  22 +var util = require('util');
  23 +var Stream = require('stream').Stream;
22 24
23 25 try {
24 26 var binding = process.binding('crypto');
25 27 var SecureContext = binding.SecureContext;
26 28 var Hmac = binding.Hmac;
27   - var Hash = binding.Hash;
  29 + var HashBinding = binding.Hash;
28 30 var Cipher = binding.Cipher;
29 31 var Decipher = binding.Decipher;
30 32 var Sign = binding.Sign;
@@ -127,6 +129,39 @@ exports.createCredentials = function(options, context) {
127 129 };
128 130
129 131
  132 +var Hash = function Hash(hash) {
  133 + this._hash = new HashBinding(hash);
  134 +
  135 + Stream.call(this);
  136 +
  137 + this.writable = true;
  138 +
  139 +};
  140 +util.inherits(Hash, Stream);
  141 +
  142 +
  143 +Hash.prototype.write = function write(data, input_encoding) {
  144 + this._hash.update(data, input_encoding);
  145 + return true;
  146 +};
  147 +
  148 +
  149 +Hash.prototype.update = function(data, input_encoding) {
  150 + return this._hash.update(data, input_encoding);
  151 +};
  152 +
  153 +
  154 +Hash.prototype.digest = function(encoding) {
  155 + return this._hash.digest(encoding);
  156 +};
  157 +
  158 +
  159 +Hash.prototype.end = function(data, input_encoding) {
  160 + if(typeof data !== 'undefined')
  161 + this.write(data, input_encoding);
  162 + this.destroySoon();
  163 +};
  164 +
130 165 exports.Hash = Hash;
131 166 exports.createHash = function(hash) {
132 167 return new Hash(hash);
62 test/simple/test-crypto-stream.js
... ... @@ -0,0 +1,62 @@
  1 +// Copyright Joyent, Inc. and other Node contributors.
  2 +//
  3 +// Permission is hereby granted, free of charge, to any person obtaining a
  4 +// copy of this software and associated documentation files (the
  5 +// "Software"), to deal in the Software without restriction, including
  6 +// without limitation the rights to use, copy, modify, merge, publish,
  7 +// distribute, sublicense, and/or sell copies of the Software, and to permit
  8 +// persons to whom the Software is furnished to do so, subject to the
  9 +// following conditions:
  10 +//
  11 +// The above copyright notice and this permission notice shall be included
  12 +// in all copies or substantial portions of the Software.
  13 +//
  14 +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15 +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16 +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  17 +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  18 +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  19 +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  20 +// USE OR OTHER DEALINGS IN THE SOFTWARE.
  21 +
  22 +var common = require('../common');
  23 +var assert = require('assert');
  24 +var path = require('path');
  25 +var fs = require('fs');
  26 +
  27 +try {
  28 + var crypto = require('crypto');
  29 +} catch (e) {
  30 + console.log('Not compiled with OPENSSL support.');
  31 + process.exit();
  32 +}
  33 +
  34 +
  35 +var filePath = path.join(common.fixturesDir, 'person.jpg');
  36 +var referenceBuffer = fs.readFileSync(filePath);
  37 +var readStreamOptions = {
  38 + flags: 'r',
  39 + bufferSize: 1024
  40 +};
  41 +
  42 +
  43 +var testHash = function(hashName, hashEncoding) {
  44 + // create reference digest
  45 + var hashBuffer = crypto.createHash(hashName);
  46 + hashBuffer.update(referenceBuffer);
  47 + var digestBuffer = hashBuffer.digest(hashEncoding);
  48 +
  49 + // create digest from stream
  50 + var hashStream = crypto.createHash(hashName);
  51 + var stream = fs.createReadStream(filePath, readStreamOptions);
  52 + stream.pipe(hashStream);
  53 + stream.on('end', function() {
  54 + var digestStream = hashStream.digest(hashEncoding);
  55 + stream.close();
  56 +
  57 + assert.deepEqual(digestStream, digestBuffer);
  58 + });
  59 +};
  60 +
  61 +
  62 +testHash('sha1', 'hex');

0 comments on commit 2c57ace

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