/
githubmirror
/
yarn
Обзор
Документация
Войти
/
githubmirror
/
yarn
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/util/crypto.js
35 строк
847 B
Simon Vocella
add prettier and prettying everything (#3401)
16 май 2017, 21:12
16 май 2017, 21:12
25890c8
Код
Авторство
О чём код?
/* @flow */ const crypto = require('crypto'); const stream = require('stream'); export function hash(content: string, type: string = 'md5'): string { return crypto.createHash(type).update(content).digest('hex'); } type HashOptions = duplexStreamOptions; export class HashStream extends stream.Transform { constructor(options?: HashOptions) { super(options); this._hash = crypto.createHash('sha1'); this._updated = false; } _hash: crypto$Hash; _updated: boolean; _transform(chunk: Buffer | string, encoding: string, callback: (error: ?Error, data?: Buffer | string) => void) { this._updated = true; this._hash.update(chunk); callback(null, chunk); } getHash(): string { return this._hash.digest('hex'); } test(sum: string): boolean { return this._updated && sum === this.getHash(); } }