# SHA-256 in Node

2016-05-14

Useful piece of code to produce a 256-bit hash value with SHA-256:

There a few JavaScript implementation of the SHA-256 hash function out there. But the easiest is usually to use Node’s built in cryptography module.

## The Code

If you just need the code, here it is:

1 | var crypto = require('crypto'); |

## Details

### Generate the hash

Import the `crypto`

module and use the `createHash`

function to generate the hash.

1 | var crypto = require('crypto'); |

### Hash your value

Use the `update`

function on the `hash`

object instance to process your input.

1 | hash.update('CodeBlocQ'); |

### Output

Use the `digest`

function on the `hash`

to ouput the value. You can pass ‘hex’, ‘binary’ or ‘base64’ to get the result in the desired encoding.

**Hex**

1 | var hex = hash.digest('hex'); |

**Binary**

1 | var bin = hash.digest('binary'); |

**Base64**

1 | var base64 = hash.digest('base64'); |