import * as plugins from './nodehash.plugins'; import * as helpers from './nodehash.helpers'; /** * creates sha256 Hash from Stream */ export const sha256FromStream = (input: plugins.stream.Stream): Promise => { const done = plugins.smartpromise.defer(); const 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 const sha256FromFile = async (filePath: string): Promise => { const absolutePath = plugins.path.resolve(filePath); const readableStream = plugins.fs.createReadStream(absolutePath); const hashResult = sha256FromStream(readableStream); return hashResult; }; /** * Computes sha256 Hash from String synchronously */ export let sha256FromStringSync = (stringArg): string => { const hash = plugins.crypto.createHash('sha256'); hash.update(stringArg); return hash.digest('hex'); }; /** * Computes sha256 Hash from String */ export const sha256FromString = async (stringArg: string): Promise => { const hash = plugins.crypto.createHash('sha256'); hash.update(stringArg); const hashResult = hash.digest('hex'); return hashResult; }; /** * computes sha265 Hash from Object */ export const sha265FromObject = async (objectArg: any): Promise => { const stringifiedObject = plugins.smartjson.Smartjson.stringify(objectArg, {}); const hashResult = await sha256FromString(stringifiedObject); return hashResult; };