smarthash/ts/nodehash.sha256.ts

49 lines
1.3 KiB
TypeScript
Raw Permalink Normal View History

import * as plugins from './nodehash.plugins';
import * as helpers from './nodehash.helpers';
2016-08-16 01:37:40 +00:00
/**
* creates sha256 Hash from Stream
*/
export let sha256FromStream = (input): Promise<string> => {
let done = plugins.q.defer<string>();
let hash = plugins.crypto.createHash('sha256');
2016-08-16 01:37:40 +00:00
hash['setEncoding']('hex');
input.pipe(hash).pipe(helpers.hashStreamPipeStop(done.resolve));
return done.promise;
2016-08-16 01:37:40 +00:00
};
/**
* 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;
};
2016-08-16 01:37:40 +00:00
/**
* Computes sha256 Hash from String synchronously
*/
export let sha256FromStringSync = stringArg => {
let hash = plugins.crypto.createHash('sha256');
hash.update(stringArg);
return hash.digest('hex');
2016-08-16 01:37:40 +00:00
};
/**
* 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;
};