Crypto Streams

In notebook:
FrontEndMasters Networking and Streams
Created at:
2017-09-30
Updated:
2017-09-30
Tags:
backend Node JS JavaScript

crypto core streams

* `crtypo.createCipher(algo, password)` - transform stream to encrypt
* `crtypo.createDecipher(algo, password)` - transform stream to decrypt
// this will protect against the replay attacks (seen before) ↴
* `crypto.createCipheriv(algo, key, iv)` - transform stream to encrypt with iv
* `crypto.createDecipheriv(algo, key, iv)` - transform stream to decrypt with
* iv
* `crypto.createHash(algo)` - transform stream to output cryptographic hash
* `crypto.createHMAC(algo, key)` - transform stream to output HMAC digest
* `crypto.createSign(algo)` - writable stream to sign messages
* `crypto.createVerify(algo)` - writable stream to verify signatures

Let's use crypto.createHash(algo)

  //	****		hash.js		****

var createHash = require('crypto').createHash

// use the encoding: 'hex' to not to have a binary format output
process.stdin.pipe(createHash('sha512', { encoding: 'hex' }))
  .pipe(process.stdout)

So with this, $ node hash.js, we type some input and get back an encrypted output:

abcd
// will return ↴ (hashed output)
etnaiotnsranartoiseratnsraioetnsaroitenfweitkarsmktrs,mktrt

Tip for uploading files

Use the hash of the file to identify the files so that if for some reason the file is uploaded again, it can be verified against the hash and only stored once.