Socket
Socket
Sign inDemoInstall

node-sha1

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

node-sha1 - npm Package Compare versions

Comparing version 0.0.1 to 0.0.2

.travis.yml

27

lib/sha1.js
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;
};

16

package.json
{
"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
})
```
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc