Comparing version 0.0.1 to 0.0.2
var crypto = require('crypto'); | ||
var stream = require('stream'); | ||
module.exports = function(str) { | ||
module.exports = function(str, callback) { | ||
var hash; | ||
hash = crypto.createHash('sha1'); | ||
if (str.pipe) { | ||
if (!callback || typeof callback !== 'function') { | ||
throw new Error('a callback is required to hash a stream'); | ||
} | ||
str.on('end', function() { | ||
callback(undefined, hash.digest('hex')); | ||
}); | ||
str.on('readable', function() { | ||
var chunk; | ||
while (null !== (chunk = str.read())) { | ||
hash.update(chunk); | ||
}; | ||
}); | ||
return; | ||
} | ||
hash.update(str); | ||
return hash.digest('hex'); | ||
var hexHash = hash.digest('hex'); | ||
if (typeof callback === 'function') { | ||
callback(undefined, hexHash); | ||
} | ||
return hexHash; | ||
}; |
{ | ||
"name": "node-sha1", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Exports a SHA1 function that uses node's crpyto module under the hood", | ||
"main": "lib/sha1.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
"test": "jasmine" | ||
}, | ||
@@ -15,10 +15,18 @@ "repository": { | ||
"sha1", | ||
"crypto" | ||
"crypto", | ||
"hash", | ||
"digest", | ||
"stream", | ||
"buffer", | ||
"string" | ||
], | ||
"author": "Michael Leaney <leahcimic@gmail.com>", | ||
"license": "BSD", | ||
"license": "ISC", | ||
"readmeFilename": "README.md", | ||
"bugs": { | ||
"url": "https://github.com/leahciMic/node-sha1/issues" | ||
}, | ||
"devDependencies": { | ||
"jasmine": "^2.3.2" | ||
} | ||
} |
@@ -1,2 +0,2 @@ | ||
# node-sha1 | ||
# node-sha1 [![Build Status](https://travis-ci.org/leahciMic/node-sha1.svg?branch=master)](https://travis-ci.org/leahciMic/node-sha1) | ||
@@ -9,3 +9,3 @@ Provides you with a convenient sha1 function that uses Node's crypto module | ||
## Use | ||
## Usage | ||
```javascript | ||
@@ -15,1 +15,65 @@ var sha1 = require('node-sha1'); | ||
``` | ||
## API | ||
`sha1(what, callback)` | ||
### what | ||
Can be a string, buffer, or even a stream (stream requires a callback). | ||
If what is a string, or a buffer the hash will be returned, if a callback | ||
is registered it will also be called with the resulting hash. | ||
If what is a stream, it will throw if a callback is not registered. Otherwise | ||
it will call the callback with the resulting hash. | ||
### callback(err, hash) | ||
`err` any errors that occurred, or undefined if no errors occurred. `hash` is | ||
the resulting hash. If a callback is registered it will be called with the | ||
resulting hash regardless if a stream, string, or buffer is used. | ||
## Examples | ||
### Use with strings | ||
```js | ||
var sha1 = require('node-sha1'); | ||
sha1('Hello World!'); # 2ef7bde608ce5404e97d5f042f95f89f1c232871 | ||
``` | ||
### Use with buffers | ||
```js | ||
var sha1 = require('node-sha1'); | ||
var buffer = new Buffer('Hello World!'); | ||
sha1(buffer); # 2ef7bde608ce5404e97d5f042f95f89f1c232871 | ||
``` | ||
### Use with streams | ||
```js | ||
var sha1 = require('node-sha1'); | ||
util.inherits(FakeStream, stream.Readable); | ||
function FakeStream() { | ||
this._finished = false; | ||
stream.Readable.call(this); | ||
} | ||
FakeStream.prototype._read = function() { | ||
if (this._finished) { | ||
this.push(null); | ||
return; | ||
} | ||
this._finished = true; | ||
this.push('Hello World!'); | ||
}; | ||
var fakeStream = new FakeStream(); | ||
sha1(fakeStream, function(err, hash) { | ||
# hash = 2ef7bde608ce5404e97d5f042f95f89f1c232871 | ||
}) | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Misc. License Issues
License(Experimental) A package's licensing information has fine-grained problems.
Found 1 instance in 1 package
Trivial Package
Supply chain riskPackages less than 10 lines of code are easily copied into your own project and may not warrant the additional supply chain risk of an external dependency.
Found 1 instance in 1 package
No tests
QualityPackage does not have any tests. This is a strong signal of a poorly maintained or low quality package.
Found 1 instance in 1 package
5225
6
0
98
2
78
0
1