Node.js 내장모듈인 Crypto를 이용해 특정 파일의 해시값을 생성하 코드
const filename = process.argv[2]; // 읽을 파일 이름을 매개 변수로 써준다
const crypto = require('crypto');
const fs = require('fs');
const hash = crypto.createHash('sha256'); // sha-256 알고리즘 사용
const input = fs.createReadStream(filename);
input.on('readable', () => {
const data = input.read();
if (data)
hash.update(data);
else {
console.log(`${hash.digest('hex')} ${filename}`);
}
});
SHA-256을 비롯해 MD5, SHA-2 등을 지원한다.
Node.js Crypto 레퍼런스 : https://nodejs.org/api/crypto.html
Crypto | Node.js v12.4.0 Documentation
Crypto# The crypto module provides cryptographic functionality that includes a set of wrappers for OpenSSL's hash, HMAC, cipher, decipher, sign, and verify functions. Use require('crypto') to access this module. const crypto = require('crypto'); const secr
nodejs.org
'프로그래밍 > JS+Node.js' 카테고리의 다른 글
[Node.js] 연습1 : 중복 파일 검색 후 특정 폴더로 이동 (0) | 2019.07.05 |
---|---|
[Node.js] SQLite3 : 인 메모리 데이터베이스(In-Memory Databases)로 사용하기 (0) | 2019.07.04 |
[Node.js] SQLite3 : run() 예외 처리 (0) | 2019.06.12 |
[자바스크립트] Ajax 통신 : XMLHttpRequest, Fetch API (0) | 2019.06.10 |