smarthash/ts/nodehash.sha256.ts

63 lines
1.8 KiB
TypeScript
Raw Normal View History

2022-06-26 09:34:09 +00:00
import * as plugins from './nodehash.plugins.js';
import * as helpers from './nodehash.helpers.js';
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
*/
2020-09-29 15:54:05 +00:00
export const sha256FromStringSync = (stringArg: string): string => {
2019-07-04 14:56:37 +00:00
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-11-21 14:20:57 +00:00
export const sha256FromString = async (stringArg: string): Promise<string> => {
2019-07-04 14:56:37 +00:00
const hash = plugins.crypto.createHash('sha256');
hash.update(stringArg);
2019-07-04 14:56:37 +00:00
const hashResult = hash.digest('hex');
return hashResult;
};
2021-03-01 01:26:55 +00:00
/**
* Computes sha256 Hash from String
*/
export const sha256FromBuffer = async (bufferArg: Buffer): Promise<string> => {
const hash = plugins.crypto.createHash('sha256');
hash.update(bufferArg);
const hashResult = hash.digest('hex');
return hashResult;
};
2019-07-04 14:56:37 +00:00
/**
* computes sha265 Hash from Object
*/
export const sha265FromObject = async (objectArg: any): Promise<string> => {
2022-06-26 09:34:09 +00:00
const stringifiedObject = plugins.smartjson.stringify(objectArg);
2019-07-04 14:56:37 +00:00
const hashResult = await sha256FromString(stringifiedObject);
return hashResult;
};