[Node. js 기초] 학습 ⑨ -- crypto

3894 단어
crypto 모듈 은 일반적인 암호 화 와 해시 알고리즘 을 제공 합 니 다.
MD5 와 SHA 1.
const crypto = require('crypto');

const hash = crypto.createHash('md5');

//        update():
hash.update('Hello, world!');
hash.update('Hello, nodejs!');
//   hash.update('Hello, world!Hello, nodejs!');

console.log(hash.digest('hex')); //7e1977739c748beac0c0fd14fd26a544

update () 방법 기본 문자열 인 코딩 UTF - 8
SHA 1 을 계산 하려 면 'md5' 를 'ha1' 로 바 꿔 야 합 니 다.
더 안전 한 sha 256 과 sha 512 도 사용 할 수 있 습 니 다.
Hmac
난수 '증강' 의 해시 알고리즘 을 사용 하 다.
const crypto = require('crypto');

const hmac = crypto.createHmac('sha256', 'secret-key');

hmac.update('Hello, world!');
hmac.update('Hello, nodejs!');

console.log(hmac.digest('hex')); // 80f7e22570...

AES
AES 는 같은 키 를 사용 하 는 대칭 암호 화 알고리즘 입 니 다.
const crypto = require('crypto');

function aesEncrypt(data, key) {
    const cipher = crypto.createCipher('aes192', key);
    var crypted = cipher.update(data, 'utf8', 'hex');
    crypted += cipher.final('hex');
    return crypted;
}

function aesDecrypt(encrypted, key) {
    const decipher = crypto.createDecipher('aes192', key);
    var decrypted = decipher.update(encrypted, 'hex', 'utf8');
    decrypted += decipher.final('utf8');
    return decrypted;
}

var data = 'Hello, this is a secret message!';
var key = 'Password!';
var encrypted = aesEncrypt(data, key);
var decrypted = aesDecrypt(encrypted, key);

console.log('Plain text: ' + data);
console.log('Encrypted text: ' + encrypted);
console.log('Decrypted text: ' + decrypted);

실행 결과
Plain text: Hello, this is a secret message!
Encrypted text: 8a944d97bdabc157a5b7a40cb180e7...
Decrypted text: Hello, this is a secret message!

Diffie-Hellman
DH 알고리즘 은 키 를 누설 하지 않 고 키 를 협상 할 수 있 는 키 교환 프로 토 콜 입 니 다.DH 알고리즘 은 수학 원 리 를 바탕 으로 합 니 다. 예 를 들 어 샤 오 밍 과 샤 오 홍 이 키 를 협상 하려 면 이렇게 할 수 있 습 니 다.
샤 오 밍 은 먼저 하나의 소수 와 하나의 밑 수 를 선택한다. 예 를 들 어 소수 p = 23, 밑 수 g = 5 (밑 수 는 선택 할 수 있다) 를 선택 한 다음 에 비밀 정수 a = 6 을 선택 하여 A = g ^ a mod p = 8 을 계산 한 다음 에 큰 소리 로 샤 오 홍 에 게 알려 준다. p = 23, g = 5, A = 8;
샤 오 홍 은 샤 오 밍 이 보 낸 p, g, A 를 받 은 후에 도 비밀 정수 b = 15 를 선택 한 다음 에 B = g ^ b mod p = 19 를 계산 하고 샤 오 밍 에 게 B = 19 를 큰 소리 로 알려 주 었 다.
샤 오 밍 은 스스로 s = B ^ a mod p = 2 를 계산 하고 샤 오 홍 도 스스로 s = A ^ b mod p = 2 를 계산 하기 때문에 최종 협상 의 키 s 는 2 이다.
이 과정 에서 키 2 는 샤 오 밍 이 샤 오 홍 에 게 알려 준 것 이 아니 라 샤 오 홍 이 샤 오 밍 에 게 알려 준 것 이 아니 라 쌍방 이 협상 하여 계산 한 것 이다.제3자 가 p = 23, g = 5, A = 8, B = 19 만 알 수 있 고 쌍방 이 선택 한 비밀 정수 a = 6 과 b = 15 를 모 르 기 때문에 키 2 를 계산 할 수 없습니다.
crypto 모듈 로 DH 알고리즘 을 구현 하려 면 다음 과 같 습 니 다.
const crypto = require('crypto');

// xiaoming's keys:
var ming = crypto.createDiffieHellman(512);
var ming_keys = ming.generateKeys();

var prime = ming.getPrime();
var generator = ming.getGenerator();

console.log('Prime: ' + prime.toString('hex'));
console.log('Generator: ' + generator.toString('hex'));

// xiaohong's keys:
var hong = crypto.createDiffieHellman(prime, generator);
var hong_keys = hong.generateKeys();

// exchange and generate secret:
var ming_secret = ming.computeSecret(hong_keys);
var hong_secret = hong.computeSecret(ming_keys);

// print secret:
console.log('Secret of Xiao Ming: ' + ming_secret.toString('hex'));
console.log('Secret of Xiao Hong: ' + hong_secret.toString('hex'));
$ node dh.js 
Prime: a8224c...deead3
Generator: 02
Secret of Xiao Ming: 695308...d519be
Secret of Xiao Hong: 695308...d519be

매번 출력 이 다 르 기 때문에 소수 선택 이 무 작위 이기 때 문 입 니 다.
crypto 상용 알고리즘

좋은 웹페이지 즐겨찾기