About
"Nodejs development Crypto Currency" is a detailed development document for cryptocurrency products, involving all aspects of developing products using NODEJS, from front-end to backstage, from server to client, from PC to mobile, encryption and decryption. The code is completely open source and the article is free to share. Related Resources See http://ebookchain.org
QQ Exchange Group: 185046161
Objective
Encryption and decryption technology, involving a wide range, here, the previous research results are summed up, through the form of charts to help memory and screening, convenient for later use. The content mainly includes two aspects, one is the scene and the algorithm, the other is the Nodejs module or the component. A total of three brain maps, specifically see:
1. Encryption and decryption overview
The following picture is done on the basis of one hour of cryptography (see Reference), the original author is Colin Percival, an expert in cryptography, the security officer of the FreeBSD project, the founder of the Tarsnap Online backup service, the author of the Scrypt Key derivation algorithm, Very worthy of reference study. Translators are @byronhe, translation contributions such a good article, including the following discussion of the scene and algorithm of practical guidance, it is worth to praise him.
This picture can tell you about the concepts, purposes, cases, and best practices of cryptography.
2. Scenarios and Algorithms
This picture is based on the Modern Cryptography Practice Guide (2015) (see reference). It can be said that on the basis of the previous picture, more specific, especially for the scene description, so that yards can be more convenient to make the right choice, worth owning. Where the number of the callout, there is a priority.
Encryption and decryption, signing and authentication in 3.Nodejs
This picture, which is mainly based on official documents and other documents (see reference), is drawn in my own understanding. If you use Nodejs, basically take a look at the diagram, you can use it directly. In particular, the default selection of ed255519
components, if you look at the above two practice, you know that this is the best choice for signing and certification, so here can be sure that the Crypto module signature and authentication or temporarily do not use it. Ebookcoin
This is the practice, see the next article. The other is the Natrium
component, which can also be used for signing and authentication, but is mainly used for asymmetric encryption and decryption. The three combinations in this brain map, according to the above experience, should be the best combination of the current Nodejs and decryption applications.
4. Fun practices
Or the use of the "encryption and decryption technology in Nodejs" in the example, set the role, the boy named Bob, his girlfriend called Alice.
Scene
Bob wants to express his long-buried voice to his girlfriend. "I Love you! "But in the face of men (men are like that?) ), sorry to say to face the export, had to encrypt the transmission. This is based on a workable assumption that they already have each other's public key, or they can simply get it.
Demand
- Encryption: cannot let others see information;
- Decryption: The girlfriend can recover and view;
- Signature: Bob can sign information to ensure that it is not tampered with;
- Authentication: The girlfriend receives the message, can Yue Heyue, confirms Bob's hair, not others ' mischief.
Scheme
Using the above three charts, we can quickly come up with technical solutions.
- Encryption and decryption techniques: The second graph shows that this kind of encryption and decryption of the original scene is very rare. Technically, it is best to use NaCl, followed by Libsodium (still NaCl behind), but the search for Github,nodejs community has no related NaCl stable package, libsodium there is a natrium (but, when writing this article, Even if the installation did not succeed, there is a successful verification, please let me know. Therefore, you can only choose to use crypto simple encryption and decryption.
- Signature and verification techniques: Of course, the best option is
ed25519
.
Coding
Create a simple Nodejs project, here's the code: https://github.com/imfly/nodejs-practice/blob/master/crypto/index.js
(1) Generate key pair
Bob does not use a random string, but instead uses a password and takes the SHA256 algorithm to generate the key pair, see the mind map, the part about the hash.
varrequire(‘crypto‘);varrequire(‘ed25519‘);var‘This is my password, you don`t guess it!‘;var hash = crypto.createHash(‘sha256‘).update(bobsPassword).digest();var bobKeypair = ed25519.MakeKeypair(hash);
(2) Encrypt and sign information
The signature is usually encrypted first.
Here, using crypto to encrypt the message, Bob's public key is used as the encryption key (but since it is the public key, who doesn't know, unless Bob only gives Alice the public key), Bob may have to tell Alice what algorithm to use to decrypt it.
varmessage‘Hi Alice, I love you!‘;var msgCiphered = cipher(‘aes192‘message//公钥进行加密,如果是Natrium,这里就是私钥加密var‘utf8‘//私钥进行签名
(3) Send a signature message to Alice
This is recount.
(4) Alice verifies and decrypts
It is usually first verified and then decrypted.
As Bob's good friend, Alice has his public key.
if(ed25519.Verify (NewBuffer (msgciphered,' UTF8 '), Signature, Bobkeypair.PublicKey)) {the//Verify function returns True by verifying that the varMsg=Decipher (' aes192 ', Bobkeypair.PublicKey, msgciphered);//Use Bob's public key to decryptConsole.Log(' signature legal, information from bob! '); Console.Log(' Bob said: ', msg);//Display information}Else{The //Verify function returned false, certainly not Bob's information.Console.Log(' signature is illegal! ');}
(5) Supplemental Code
The encryption and decryption method of the crypto used above:
//Decryption function (algorithm, key, buffer){ varencrypted ="";varCIP = Crypto.createcipher (algorithm, key); encrypted + = cip.update (buffer,' UTF8 ',' Hex '); Encrypted + = CIP.Final(' Hex ');returnencrypted;}//Decryption function decipher(algorithm, key, encrypted){ vardecrypted ="";varDecipher = Crypto.createdecipher (algorithm, key); decrypted + = Decipher.update (encrypted,' Hex ',' UTF8 '); decrypted + = decipher.Final(' UTF8 ');returndecrypted;}
(6) Running example
Use the following command to run the above code:
$ https://github.com/imfly/nodejs-practice$ cd nodejs-practice$ npm install$ node crypto/
Output Result:
签名合法,信息来自Bob!Bob said: I love you!
Link
This series of articles is updated instantly, if interested, through Star
the collection, ^-^
Source Address: Https://github.com/imfly/bitcoin-on-nodejs
ebook reading: http://bitcoin-on-nodejs.ebookchain.org
Reference
Ed25519 third-party components
ED25519 official website
A practical Guide to Modern Cryptography (2015)
One hour of cryptography must be known
Talking about the crypto module in Nodejs
"Nodejs development Crypto currency" 10:3 map to give you a complete grasp of encryption and decryption technology