Andrew Kim
Andrew Kim's Blog

Andrew Kim's Blog

A quick method to verify duplicate entries using hash

Andrew Kim's photo
Andrew Kim
·Nov 10, 2021·

1 min read

I kept coming back to creating methods that checked if a combination of fields is unique, and ended up creating a simple method, and wanted record it here.

This is for TypeScript. I am a newbie in coding world, so if there is a suggestion or something better, I'd love to hear!

import { createHash } from 'crypto';

function getHash(input: string[]) {
    const _now = new Date().toString();
    const _hashString = input.join(' ') + _now;
    const hash = createHash('sha256');
    hash.update(_hashString);
    return hash.digest('hex').toString();
  }

You can pass an array of strings to the method to create a unique hash.

getHash([item.serialNumber, item.deviceModel])

Just a nice code snippet.

Happy Coding!

 
Share this