smarthash/ts/nodehash.sha256.ts

53 lines
1.5 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
*/
2019-07-04 14:56:37 +00:00
export const sha256FromStream = (input: plugins.stream.Stream): Promise<string> => {
const done = plugins.smartpromise.defer<string>();
const hash = plugins.crypto.createHash('sha256');
2016-08-16 01:37:40 +00:00
2019-07-04 14:56:37 +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;
*/
2019-07-04 14:56:37 +00:00
export const sha256FromFile = async (filePath: string): Promise<string> => {
const absolutePath = plugins.path.resolve(filePath);
const readableStream = plugins.fs.createReadStream(absolutePath);
const hashResult = sha256FromStream(readableStream);
return hashResult;
};
2016-08-16 01:37:40 +00:00
/**
* Computes sha256 Hash from String synchronously
*/
2019-07-04 14:56:37 +00:00
export let sha256FromStringSync = (stringArg): string => {
const 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
*/
2019-07-04 14:56:37 +00:00
export const sha256FromString = async(stringArg: string): Promise<string> => {
const hash = plugins.crypto.createHash('sha256');
hash.update(stringArg);
2019-07-04 14:56:37 +00:00
const hashResult = hash.digest('hex');
return hashResult;
};
/**
* computes sha265 Hash from Object
*/
export const sha265FromObject = async (objectArg: any): Promise<string> => {
const stringifiedObject = plugins.smartjson.Smartjson.stringify(objectArg, {});
const hashResult = await sha256FromString(stringifiedObject);
return hashResult;
};