Strattic Developers by Elementor

Using a Hashing API to Improve Security

Occasionaly, you’ll need to hash some information: an email, a password, your grandma’s recipe for jambalaya 😋 …

If you need to hash any data (like this integration), you can do something like the following…

You’ll need to use this NPM package to make this hashing example work.

And you’ll want to include the node_modules/js-sha256 directory in your deployment, use this article for reference

Install

For this to work, you need to log into your Serverless Framework dashboard and create a `hashKey` key-value pair:

  1. Deploy your application with sls deploy
  2. Login to https://app.serverless.com/ (or from the command line, do sls login).
  3. Got to your app function byol-hashing-api, click on the “parameters” tab.
  4. Add a key/value pair, with the “name” as hashKey and the “value” as whatever you want to hash the value to.
  5. Click “Add”.

Now, you will be able to POST to the endpoint given.

Send a JSON body in the request like:

{
	"email": "me@me.com"
}

And you should get back a response like:

{
	"key": "5c442a75f1c1f76edfa525d9f43f19538f3b64812d0caabdfda54d2d17149fde"
}

That is your hashed key.


“`This is the serverless.yml file:

org: your-org
app: byol-hashing-api
service: byol-hashing-api

frameworkVersion: '2'

provider:
  name: aws
  runtime: nodejs12.x
  lambdaHashingVersion: '20201221'
  environment:
    HASH_KEY: ${param:hashKey}

functions:
  hashit:
    handler: handler.hashit
    events:
      - httpApi:
          path: /
          method: post
package:
  patterns:
    - '!node_modules/**'
    - 'node_modules/js-sha256/**'

This is the handler.js file:

"use strict";
const sha256 = require('js-sha256').sha256;

// The key to us for hashing.
const hashKey = process.env.HASH_KEY;

module.exports.hashit = async (event) => {
  // Parse the JSON body.
  const body = JSON.parse(event.body)

  // Send back the hashed email.
  return {
    statusCode: 200,
    body: JSON.stringify(
      {
        key: sha256.hmac(hashKey, body.email)
      },
      null,
      2
    ),
  };
};

Black Friday 20% Off
days
hr
min
sec