DEV Community

Haaris Iqubal for Recoding

Posted on • Updated on • Originally published at Medium

Hashing your Data in Deno Using Bcrypt

Encryption has always been important aspect of Human Life as it help use to send data from one part of then world to another through different agent but its secret will not reveled. So how would your implement encryption into Deno app let unwrap this enigma… We are using bcrypt module from Deno Third Parties library, so the show its working we are going to implement a Deno application so create app.ts file and type the following Boilerplate code to start your application up and running.


import { Application, Router } from "https://deno.land/x/oak/mod.ts";const app = new Application();
const router = new Router();
 router
  .get("/",(ctx) => {
   ctx.response.body = "Router has been created";
   // Implement your code
   })
  .post("/addPost", (ctx) => {
   ctx.response.body = "This is port request";
   // Implement your code   
   });
app.use(router.routes());
app.use(router.allowedMethods());
console.log('App is listening to 8000 PORT');
app.listen({port: 8000});

After writing boiler plate code we need to require our bcrypt module as


import * as bcrypt from "https://deno.land/x/bcrypt/mod.ts";

After importing bcrypt we can now use it as bcrypt every where we want so lets just add bcrypt into our “POST” request path as mentioned below


.post("/addPost", (ctx) => { 
 const salt = bcrypt.genSaltSync(8);
 const hash = bcrypt.hashSync("Type Data you want to hash", salt);
 console.log(hash);
 })

Our final result gonna look like this

Alt Text

This bcrypt function can also implement as async function as mentioned below


.post("/addPost", async (ctx) => { 
 const salt = await bcrypt.genSaltSync(8);
 const hash = await bcrypt.hashSync("Type Data you want to hash", salt);
 console.log(hash);
 })

So this how we can implement hashing inside Deno Application. Here is our final code gonna look like.


import { Application, Router } from "https://deno.land/x/oak/mod.ts";

const app = new Application();

const router = new Router();

 router
  .get("/",(ctx) => {
   ctx.response.body = "Router has been created";
   // Implement your code
   })
  .post("/addPost", async (ctx) => { 
    const salt = await bcrypt.genSaltSync(8);
    const hash = await bcrypt.hashSync("Type Data you want to hash", salt);
  console.log(hash);
  })

app.use(router.routes());

app.use(router.allowedMethods());

console.log('App is listening to 8000 PORT');

app.listen({port: 8000});

Top comments (0)