implementation is ready
This commit is contained in:
parent
17ccc27244
commit
e2b90b7d30
22
README.md
22
README.md
@ -10,6 +10,24 @@ We recommend the use of TypeScript for best in class intellisense.
|
||||
```typescript
|
||||
import * as nodehash from "nodehash";
|
||||
|
||||
let sha256 = new nodehash.sha256();
|
||||
let myHashedString = sha256.fromString();
|
||||
// from stream
|
||||
let readStream = fs.createReadStream("./somefile.txt");
|
||||
nodehash.sha256FromStream(readStream)
|
||||
.then((resultString){
|
||||
console.log(resultString); // prints hash of the file
|
||||
});
|
||||
|
||||
// from file
|
||||
nodehash.sha256FromFile("./somefile.txt")
|
||||
.then((resultString){
|
||||
console.log(resultString); // prints hash of the file
|
||||
});
|
||||
|
||||
// from string
|
||||
nodehash.sha256FromString("some weird random string");
|
||||
.then((resultString){
|
||||
console.log(resultString); // prints hash of the file
|
||||
});
|
||||
|
||||
let hashString = nodehash.sha256FromStringSync("some weird random string")
|
||||
```
|
1
dist/index.d.ts
vendored
1
dist/index.d.ts
vendored
@ -0,0 +1 @@
|
||||
export * from "./nodehash.sha256";
|
6
dist/index.js
vendored
6
dist/index.js
vendored
@ -1,2 +1,6 @@
|
||||
"use strict";
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiIn0=
|
||||
function __export(m) {
|
||||
for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
|
||||
}
|
||||
__export(require("./nodehash.sha256"));
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW5kZXguanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuLi90cy9pbmRleC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiOzs7O0FBRUEsaUJBQWMsbUJBQW1CLENBQUMsRUFBQSJ9
|
2
dist/nodehash.helpers.d.ts
vendored
Normal file
2
dist/nodehash.helpers.d.ts
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
/// <reference types="node" />
|
||||
export declare let hashStreamPipeStop: (resolveFuntion: any) => NodeJS.ReadWriteStream;
|
13
dist/nodehash.helpers.js
vendored
Normal file
13
dist/nodehash.helpers.js
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
"use strict";
|
||||
const plugins = require("./nodehash.plugins");
|
||||
exports.hashStreamPipeStop = (resolveFuntion) => {
|
||||
let forEach = (chunk, enc, cb) => {
|
||||
resolveFuntion(chunk.toString("utf8"));
|
||||
cb(null, chunk);
|
||||
};
|
||||
let atEnd = (cb) => {
|
||||
cb();
|
||||
};
|
||||
return plugins.through2(forEach, atEnd);
|
||||
};
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibm9kZWhhc2guaGVscGVycy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL25vZGVoYXNoLmhlbHBlcnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLE1BQVksT0FBTyxXQUFNLG9CQUFvQixDQUFDLENBQUE7QUFFbkMsMEJBQWtCLEdBQUcsQ0FBQyxjQUFjO0lBQzNDLElBQUksT0FBTyxHQUFHLENBQUMsS0FBWSxFQUFDLEdBQUcsRUFBQyxFQUFFO1FBQzlCLGNBQWMsQ0FBQyxLQUFLLENBQUMsUUFBUSxDQUFDLE1BQU0sQ0FBQyxDQUFDLENBQUM7UUFDdkMsRUFBRSxDQUFDLElBQUksRUFBQyxLQUFLLENBQUMsQ0FBQztJQUNuQixDQUFDLENBQUM7SUFFRixJQUFJLEtBQUssR0FBRyxDQUFDLEVBQUU7UUFDWCxFQUFFLEVBQUUsQ0FBQztJQUNULENBQUMsQ0FBQztJQUVGLE1BQU0sQ0FBQyxPQUFPLENBQUMsUUFBUSxDQUFDLE9BQU8sRUFBQyxLQUFLLENBQUMsQ0FBQztBQUMzQyxDQUFDLENBQUMifQ==
|
5
dist/nodehash.plugins.d.ts
vendored
5
dist/nodehash.plugins.d.ts
vendored
@ -1,2 +1,7 @@
|
||||
import "typings-global";
|
||||
export import crypto = require("crypto");
|
||||
export import fs = require("fs");
|
||||
export import path = require("path");
|
||||
export import q = require("q");
|
||||
export import stream = require("stream");
|
||||
export import through2 = require("through2");
|
||||
|
7
dist/nodehash.plugins.js
vendored
7
dist/nodehash.plugins.js
vendored
@ -1,4 +1,9 @@
|
||||
"use strict";
|
||||
require("typings-global");
|
||||
exports.crypto = require("crypto");
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibm9kZWhhc2gucGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL25vZGVoYXNoLnBsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sZ0JBQWdCLENBQUMsQ0FBQTtBQUNWLGNBQU0sV0FBVyxRQUFRLENBQUMsQ0FBQyJ9
|
||||
exports.fs = require("fs");
|
||||
exports.path = require("path");
|
||||
exports.q = require("q");
|
||||
exports.stream = require("stream");
|
||||
exports.through2 = require("through2");
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibm9kZWhhc2gucGx1Z2lucy5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbIi4uL3RzL25vZGVoYXNoLnBsdWdpbnMudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sZ0JBQWdCLENBQUMsQ0FBQTtBQUNWLGNBQU0sV0FBVyxRQUFRLENBQUMsQ0FBQztBQUMzQixVQUFFLFdBQVcsSUFBSSxDQUFDLENBQUM7QUFDbkIsWUFBSSxXQUFXLE1BQU0sQ0FBQyxDQUFDO0FBQ3ZCLFNBQUMsV0FBVyxHQUFHLENBQUMsQ0FBQztBQUNqQixjQUFNLFdBQVcsUUFBUSxDQUFDLENBQUM7QUFDM0IsZ0JBQVEsV0FBVyxVQUFVLENBQUMsQ0FBQyJ9
|
18
dist/nodehash.sha256.d.ts
vendored
Normal file
18
dist/nodehash.sha256.d.ts
vendored
Normal file
@ -0,0 +1,18 @@
|
||||
/// <reference types="q" />
|
||||
import * as plugins from "./nodehash.plugins";
|
||||
/**
|
||||
* creates sha256 Hash from Stream
|
||||
*/
|
||||
export declare let sha256FromStream: (input: any) => plugins.q.Promise<{}>;
|
||||
/**
|
||||
* creates sha256 Hash from File;
|
||||
*/
|
||||
export declare let sha256FromFile: (filePath: string) => plugins.q.Promise<{}>;
|
||||
/**
|
||||
* Computes sha256 Hash from String synchronously
|
||||
*/
|
||||
export declare let sha256FromStringSync: (stringArg: any) => any;
|
||||
/**
|
||||
* Computes sha256 Hash from String
|
||||
*/
|
||||
export declare let sha256FromString: (stringArg: any) => plugins.q.Promise<{}>;
|
48
dist/nodehash.sha256.js
vendored
Normal file
48
dist/nodehash.sha256.js
vendored
Normal file
@ -0,0 +1,48 @@
|
||||
"use strict";
|
||||
const plugins = require("./nodehash.plugins");
|
||||
const helpers = require("./nodehash.helpers");
|
||||
/**
|
||||
* creates sha256 Hash from Stream
|
||||
*/
|
||||
exports.sha256FromStream = (input) => {
|
||||
let done = plugins.q.defer();
|
||||
let hash = plugins.crypto.createHash('sha256');
|
||||
hash["setEncoding"]("hex");
|
||||
input
|
||||
.pipe(hash)
|
||||
.pipe(helpers.hashStreamPipeStop(done.resolve));
|
||||
return done.promise;
|
||||
};
|
||||
/**
|
||||
* creates sha256 Hash from File;
|
||||
*/
|
||||
exports.sha256FromFile = (filePath) => {
|
||||
let done = plugins.q.defer();
|
||||
let absolutePath = plugins.path.resolve(filePath);
|
||||
let readableStream = plugins.fs.createReadStream(absolutePath);
|
||||
exports.sha256FromStream(readableStream)
|
||||
.then((resultHashString) => {
|
||||
done.resolve(resultHashString);
|
||||
});
|
||||
return done.promise;
|
||||
};
|
||||
/**
|
||||
* Computes sha256 Hash from String synchronously
|
||||
*/
|
||||
exports.sha256FromStringSync = (stringArg) => {
|
||||
let hash = plugins.crypto.createHash('sha256');
|
||||
hash.update(stringArg);
|
||||
return hash.digest("hex");
|
||||
};
|
||||
/**
|
||||
* Computes sha256 Hash from String
|
||||
*/
|
||||
exports.sha256FromString = (stringArg) => {
|
||||
let done = plugins.q.defer();
|
||||
let hash = plugins.crypto.createHash('sha256');
|
||||
hash.update(stringArg);
|
||||
let hashResult = hash.digest("hex");
|
||||
done.resolve(hashResult);
|
||||
return done.promise;
|
||||
};
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibm9kZWhhc2guc2hhMjU2LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vdHMvbm9kZWhhc2guc2hhMjU2LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7QUFBQSxNQUFZLE9BQU8sV0FBTSxvQkFBb0IsQ0FBQyxDQUFBO0FBQzlDLE1BQVksT0FBTyxXQUFNLG9CQUFvQixDQUFDLENBQUE7QUFFOUM7O0dBRUc7QUFDUSx3QkFBZ0IsR0FBRyxDQUFDLEtBQUs7SUFDaEMsSUFBSSxJQUFJLEdBQUcsT0FBTyxDQUFDLENBQUMsQ0FBQyxLQUFLLEVBQUUsQ0FBQztJQUM3QixJQUFJLElBQUksR0FBRyxPQUFPLENBQUMsTUFBTSxDQUFDLFVBQVUsQ0FBQyxRQUFRLENBQUMsQ0FBQztJQUUvQyxJQUFJLENBQUMsYUFBYSxDQUFDLENBQUMsS0FBSyxDQUFDLENBQUM7SUFDM0IsS0FBSztTQUNBLElBQUksQ0FBQyxJQUFJLENBQUM7U0FDVixJQUFJLENBQUMsT0FBTyxDQUFDLGtCQUFrQixDQUFDLElBQUksQ0FBQyxPQUFPLENBQUMsQ0FBQyxDQUFDO0lBQ3BELE1BQU0sQ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ENBQUMsVUFBVSxDQUFDLFFBQVEsQ0FBQyxDQUFDO0lBQy9DLElBQUksQ0FBQyxNQUFNLENBQUMsU0FBUyxDQUFDLENBQUM7SUFDdkIsSUFBSSxVQUFVLEdBQUcsSUFBSSxDQUFDLE1BQU0sQ0FBQyxLQUFLLENBQUMsQ0FBQztJQUNwQyxJQUFJLENBQUMsT0FBTyxDQUFDLFVBQVUsQ0FBQyxDQUFDO0lBQ3pCLE1BQU0sQ0FBQyxJQUFJLENBQUMsT0FBTyxDQUFDO0FBQ3hCLENBQUMsQ0FBQyJ9
|
@ -14,7 +14,7 @@
|
||||
"keywords": [
|
||||
"hash",
|
||||
"node",
|
||||
"sha"
|
||||
"sha265"
|
||||
],
|
||||
"author": "Lossless GmbH",
|
||||
"license": "MIT",
|
||||
@ -28,6 +28,10 @@
|
||||
"typings-test": "^1.0.1"
|
||||
},
|
||||
"dependencies": {
|
||||
"@types/q": "0.x.x",
|
||||
"@types/through2": "0.x.x",
|
||||
"q": "^1.4.1",
|
||||
"through2": "^2.0.1",
|
||||
"typings-global": "^1.0.6"
|
||||
}
|
||||
}
|
||||
|
1
test/test.d.ts
vendored
1
test/test.d.ts
vendored
@ -0,0 +1 @@
|
||||
import "typings-test";
|
47
test/test.js
47
test/test.js
@ -1 +1,46 @@
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IiJ9
|
||||
"use strict";
|
||||
require("typings-test");
|
||||
const should = require("should");
|
||||
const fs = require("fs");
|
||||
const nodehash = require("../dist/index");
|
||||
describe("nodehash", function () {
|
||||
describe("sha256FromStringSync", function () {
|
||||
it("should convert a String to sha256 hash synchronously", function () {
|
||||
let testHash = nodehash.sha256FromStringSync("test");
|
||||
let testHash2 = nodehash.sha256FromStringSync("testString");
|
||||
let testHash3 = nodehash.sha256FromStringSync("test");
|
||||
should.equal(testHash, testHash3);
|
||||
should.notEqual(testHash, "test");
|
||||
});
|
||||
});
|
||||
describe("sha256fromStringSync", function () {
|
||||
it("should convert a String to sha256 hash synchronously", function (done) {
|
||||
nodehash.sha256FromString("test")
|
||||
.then(resultString => {
|
||||
let compareString = nodehash.sha256FromStringSync("test");
|
||||
should.equal(resultString, compareString);
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
describe("sha256fromStream", function () {
|
||||
it("should convert a Stream to sha256 and resolve a promise with result", function (done) {
|
||||
let readStream = fs.createReadStream("./test/testImageForHash.jpg");
|
||||
nodehash.sha256FromStream(readStream)
|
||||
.then((resultString) => {
|
||||
should.equal(resultString, "45b80413ed93acb495691186ce61850449439f9183352b9bff96d5533fa1046c");
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
describe("sha256fromFile", function () {
|
||||
it("should convert a Stream to sha256 and resolve a promise with result", function (done) {
|
||||
nodehash.sha256FromFile("./test/testImageForHash.jpg")
|
||||
.then((resultString) => {
|
||||
should.equal(resultString, "45b80413ed93acb495691186ce61850449439f9183352b9bff96d5533fa1046c");
|
||||
done();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidGVzdC5qcyIsInNvdXJjZVJvb3QiOiIiLCJzb3VyY2VzIjpbInRlc3QudHMiXSwibmFtZXMiOltdLCJtYXBwaW5ncyI6IjtBQUFBLFFBQU8sY0FBYyxDQUFDLENBQUE7QUFDdEIsTUFBTyxNQUFNLFdBQVcsUUFBUSxDQUFDLENBQUM7QUFDbEMsTUFBTyxFQUFFLFdBQVcsSUFBSSxDQUFDLENBQUM7QUFFMUIsTUFBWSxRQUFRLFdBQU0sZUFBZSxDQUFDLENBQUE7QUFFMUMsUUFBUSxDQUFDLFVBQVUsRUFBQztJQUNoQixRQUFRLENBQUMsc0JBQXNCLEVBQUM7UUFDNUI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BQyxDQUFDO1lBQ3BFLFFBQVEsQ0FBQyxnQkFBZ0IsQ0FBQyxVQUFVLENBQUM7aUJBQ2hDLElBQUksQ0FBQyxDQUFDLFlBQW1CO2dCQUN0QixNQUFNLENBQUMsS0FBSyxDQUFDLFlBQVksRUFBQyxrRUFBa0UsQ0FBQyxDQUFBO2dCQUM3RixJQUFJLEVBQUUsQ0FBQztZQUNYLENBQUMsQ0FBQyxDQUFDO1FBQ1gsQ0FBQyxDQUFDLENBQUE7SUFDTixDQUFDLENBQUMsQ0FBQztJQUNILFFBQVEsQ0FBQyxnQkFBZ0IsRUFBQztRQUN0QixFQUFFLENBQUMscUVBQXFFLEVBQUMsVUFBUyxJQUFJO1lBQ2xGLFFBQVEsQ0FBQyxjQUFjLENBQUMsNkJBQTZCLENBQUM7aUJBQ2pELElBQUksQ0FBQyxDQUFDLFlBQW1CO2dCQUN0QixNQUFNLENBQUMsS0FBSyxDQUFDLFlBQVksRUFBQyxrRUFBa0UsQ0FBQyxDQUFBO2dCQUM3RixJQUFJLEVBQUUsQ0FBQztZQUNYLENBQUMsQ0FBQyxDQUFDO1FBQ1gsQ0FBQyxDQUFDLENBQUE7SUFDTixDQUFDLENBQUMsQ0FBQztBQUNQLENBQUMsQ0FBQyxDQUFDIn0=
|
46
test/test.ts
46
test/test.ts
@ -0,0 +1,46 @@
|
||||
import "typings-test";
|
||||
import should = require("should");
|
||||
import fs = require("fs");
|
||||
|
||||
import * as nodehash from "../dist/index";
|
||||
|
||||
describe("nodehash",function(){
|
||||
describe("sha256FromStringSync",function(){
|
||||
it("should convert a String to sha256 hash synchronously",function(){
|
||||
let testHash = nodehash.sha256FromStringSync("test");
|
||||
let testHash2 = nodehash.sha256FromStringSync("testString");
|
||||
let testHash3 = nodehash.sha256FromStringSync("test");
|
||||
should.equal(testHash,testHash3);
|
||||
should.notEqual(testHash,"test");
|
||||
})
|
||||
});
|
||||
describe("sha256fromStringSync",function(){
|
||||
it("should convert a String to sha256 hash synchronously",function(done){
|
||||
nodehash.sha256FromString("test")
|
||||
.then(resultString => {
|
||||
let compareString = nodehash.sha256FromStringSync("test");
|
||||
should.equal(resultString,compareString);
|
||||
done();
|
||||
})
|
||||
})
|
||||
});
|
||||
describe("sha256fromStream",function(){
|
||||
it("should convert a Stream to sha256 and resolve a promise with result",function(done){
|
||||
let readStream = fs.createReadStream("./test/testImageForHash.jpg");
|
||||
nodehash.sha256FromStream(readStream)
|
||||
.then((resultString:string) => {
|
||||
should.equal(resultString,"45b80413ed93acb495691186ce61850449439f9183352b9bff96d5533fa1046c")
|
||||
done();
|
||||
});
|
||||
})
|
||||
});
|
||||
describe("sha256fromFile",function(){
|
||||
it("should convert a Stream to sha256 and resolve a promise with result",function(done){
|
||||
nodehash.sha256FromFile("./test/testImageForHash.jpg")
|
||||
.then((resultString:string) => {
|
||||
should.equal(resultString,"45b80413ed93acb495691186ce61850449439f9183352b9bff96d5533fa1046c")
|
||||
done();
|
||||
});
|
||||
})
|
||||
});
|
||||
});
|
BIN
test/testImageForHash.jpg
Normal file
BIN
test/testImageForHash.jpg
Normal file
Binary file not shown.
After Width: | Height: | Size: 1.7 MiB |
@ -1 +1,3 @@
|
||||
import * as plugins from "./nodehash.plugins";
|
||||
|
||||
export * from "./nodehash.sha256";
|
15
ts/nodehash.helpers.ts
Normal file
15
ts/nodehash.helpers.ts
Normal file
@ -0,0 +1,15 @@
|
||||
import * as plugins from "./nodehash.plugins";
|
||||
|
||||
export let hashStreamPipeStop = (resolveFuntion) => {
|
||||
let forEach = (chunk:Buffer,enc,cb) => {
|
||||
resolveFuntion(chunk.toString("utf8"));
|
||||
cb(null,chunk);
|
||||
};
|
||||
|
||||
let atEnd = (cb) => {
|
||||
cb();
|
||||
};
|
||||
|
||||
return plugins.through2(forEach,atEnd);
|
||||
};
|
||||
|
@ -1,2 +1,7 @@
|
||||
import "typings-global";
|
||||
export import crypto = require("crypto");
|
||||
export import crypto = require("crypto");
|
||||
export import fs = require("fs");
|
||||
export import path = require("path");
|
||||
export import q = require("q");
|
||||
export import stream = require("stream");
|
||||
export import through2 = require("through2");
|
||||
|
51
ts/nodehash.sha256.ts
Normal file
51
ts/nodehash.sha256.ts
Normal file
@ -0,0 +1,51 @@
|
||||
import * as plugins from "./nodehash.plugins";
|
||||
import * as helpers from "./nodehash.helpers";
|
||||
|
||||
/**
|
||||
* creates sha256 Hash from Stream
|
||||
*/
|
||||
export let sha256FromStream = (input) => {
|
||||
let done = plugins.q.defer();
|
||||
let hash = plugins.crypto.createHash('sha256');
|
||||
|
||||
hash["setEncoding"]("hex");
|
||||
input
|
||||
.pipe(hash)
|
||||
.pipe(helpers.hashStreamPipeStop(done.resolve));
|
||||
return done.promise;
|
||||
};
|
||||
|
||||
/**
|
||||
* creates sha256 Hash from File;
|
||||
*/
|
||||
export let sha256FromFile = (filePath:string) => {
|
||||
let done = plugins.q.defer();
|
||||
let absolutePath = plugins.path.resolve(filePath);
|
||||
let readableStream = plugins.fs.createReadStream(absolutePath);
|
||||
sha256FromStream(readableStream)
|
||||
.then((resultHashString) => {
|
||||
done.resolve(resultHashString);
|
||||
});
|
||||
return done.promise;
|
||||
}
|
||||
|
||||
/**
|
||||
* Computes sha256 Hash from String synchronously
|
||||
*/
|
||||
export let sha256FromStringSync = (stringArg) => {
|
||||
let hash = plugins.crypto.createHash('sha256');
|
||||
hash.update(stringArg);
|
||||
return hash.digest("hex");
|
||||
};
|
||||
|
||||
/**
|
||||
* Computes sha256 Hash from String
|
||||
*/
|
||||
export let sha256FromString = (stringArg) => {
|
||||
let done = plugins.q.defer();
|
||||
let hash = plugins.crypto.createHash('sha256');
|
||||
hash.update(stringArg);
|
||||
let hashResult = hash.digest("hex");
|
||||
done.resolve(hashResult);
|
||||
return done.promise;
|
||||
};
|
Loading…
Reference in New Issue
Block a user