Node. crypto module of JS series learning

Posted by mecha_godzilla on Mon, 03 Jan 2022 01:22:12 +0100

Catalogue of series articles

One stage
1. fs of node basic module
2. stream of node basic module
3. http of node basic module
4. crypto of node basic module
Two stage koa framework
1. Getting started with koa and how koa handles different URLs
2. Koo's template engine nunjuks
3.koa realize MVC mode
Three stages
1.Node+WebSocket implements a chat room
Four stages
1. How to use REST in koa
2. Write REST compliant interfaces in koa

Tip: after writing the article, the directory can be generated automatically. For how to generate it, please refer to the help document on the right

preface

Tip: Here you can add the general contents to be recorded in this article:
For example, with the continuous development of artificial intelligence, machine learning technology is becoming more and more important. Many people have started learning machine learning. This paper introduces the basic content of machine learning.

Tip: the following is the main content of this article. The following cases can be used for reference

1, What is the crypto module?

  1. The purpose of crypto module is to provide general encryption and hash algorithms. It's not impossible to implement these functions with pure JavaScript code, but it will be very slow.
  2. After Nodejs implements these algorithms in C/C + +, it is exposed as a JavaScript interface through the module cypto, which is convenient to use and fast to run.
  3. Simply put: crypto is a library that includes encryption and decryption algorithms.
  4. The node crypto module provides encryption functions, including a complete package of OpenSSL hash, HMAC, encryption, decryption, signature and verification functions

2, crypto support algorithm

1.MD5,sha1,sha512

MD5 is a commonly used hash algorithm, which is used to give any data a "signature". This signature is usually represented by a hexadecimal string
be careful:

const crypto = require('crypto');
const hash = crypto.createHash('md5');
// There are three different options for creating a hash. md5 is widely used
// If you want to calculate sha1, just change 'md5' to 'sha1'
// You can also use the more secure sha256 and sha512
// update() can be called any number of times to pass in the string or binary to be encrypted

hash.update('Hello');
// Encrypt the output in the specified format
// hash.digest() can select 'bin' and hex to correspond to binary and hexadecimal respectively
const newStr = hash.digest('hex')
console.log(newStr);

2.Hmac

  • Hmac algorithm is also a hash algorithm, which can use hash algorithms such as MD5 or SHA1. The difference is that Hmac also needs a key
  • As long as the key changes, the same input data will get different signatures. Therefore, Hmac can be understood as a hash algorithm "enhanced" by random numbers
  • As for the secret key, we can use the random generation mode. In this way, the encryption level will make great progress
const secretKey = 'qwerty'
const hmac = crypto.createHmac('sha256', secretKey);
hmac.update('Hello');
console.log(hmac.digest('hex'));
//c51283c48610dd9b433ce4bf9e7b0b44b808f98bb056fca45953101b1d8fc973

//Change secretKey
const secretKey = 'zxcvbn'
const hmac = crypto.createHmac('sha256', secretKey);
hmac.update('Hello');
console.log(hmac.digest('hex'));

//f179a8eb3d0dec61ccc9fafcfc64e901437947708e19b9efa57383638923cfa9

3.AES

AES is a commonly used symmetric encryption algorithm, which uses the same key for encryption and decryption. The crypto module provides AES support, but you need to encapsulate the functions yourself
// encryption
function aesEncrypt(data, key) {
    const cipher = crypto.createCipher('aes192', key);
    var crypted = cipher.update(data, 'utf8', 'hex');
    crypted += cipher.final('hex');
    return crypted;
}


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

// String to be encrypted
var data = 'Hello, this is a secret message!';
// Key key
var key = 'Password!';
// Encryption function
var encrypted = aesEncrypt(data, key);
// Decryption function
var decrypted = aesDecrypt(encrypted, key);

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

summary

This article mainly introduces several common password encryption methods of node, hoping to be helpful to you.

Topics: node.js md5