Skip to content
Permalink
Browse files

feat: stringId(), hashUtil (md5, ...)

  • Loading branch information...
kirillgroshkov committed May 18, 2019
1 parent 8ab6ccf commit 811008467e9563f6f8784bbdc7adce23b1450253
Showing with 95 additions and 10 deletions.
  1. +2 −1 package.json
  2. +16 −0 src/index.ts
  3. +15 −0 src/security/hash.util.test.ts
  4. +28 −0 src/security/hash.util.ts
  5. +20 −0 src/security/id.util.ts
  6. +14 −9 yarn.lock
@@ -6,7 +6,8 @@
"@types/hapi__joi": "^15.0.1",
"@types/lru-cache": "^5.1.0",
"lru-cache": "^5.1.1",
"luxon": "^1.13.2"
"luxon": "^1.13.2",
"nanoid": "^2.0.2"
},
"peerDependencies": {
"@hapi/joi": "^15.0.0"
@@ -1,4 +1,13 @@
import { processSharedUtil } from './infra/process.shared.util'
import {
base64ToBuffer,
base64ToString,
bufferToBase64,
hash,
md5,
stringToBase64,
} from './security/hash.util'
import { stringId } from './security/id.util'
import { requireEnvKeys } from './util/env.util'
import { LRUMemoCache } from './util/lruMemoCache'
import { unzipBuffer, unzipToString, zipBuffer, zipString } from './util/zip.util'
@@ -68,4 +77,11 @@ export {
unzipToString,
requireEnvKeys,
LRUMemoCache,
stringId,
md5,
hash,
stringToBase64,
base64ToString,
bufferToBase64,
base64ToBuffer,
}
@@ -0,0 +1,15 @@
import { base64ToString, md5, stringToBase64 } from './hash.util'

test('md5', () => {
const plain = 'hello!@#123'
const m = md5(plain)
expect(m).toBe('41f871086829ceb41c02d2f99e11ddd0')
})

test('base64', () => {
const plain = 'hello!@#123'
const enc = stringToBase64(plain)
expect(enc).toBe('aGVsbG8hQCMxMjM=')
const dec = base64ToString(enc)
expect(dec).toBe(plain)
})
@@ -0,0 +1,28 @@
import * as crypto from 'crypto'

export function md5 (s: string | Buffer): string {
return hash(s, 'md5')
}

export function hash (s: string | Buffer, algorithm: string): string {
return crypto
.createHash(algorithm)
.update(s)
.digest('hex')
}

export function stringToBase64 (s: string): string {
return Buffer.from(s, 'utf8').toString('base64')
}

export function base64ToString (strBase64: string): string {
return Buffer.from(strBase64, 'base64').toString('utf8')
}

export function bufferToBase64 (b: Buffer): string {
return b.toString('base64')
}

export function base64ToBuffer (strBase64: string): Buffer {
return Buffer.from(strBase64, 'base64')
}
@@ -0,0 +1,20 @@
type NanoidGenerate = (alphabet: string, length?: number) => string

const nanoidGenerate = require('nanoid/generate') as NanoidGenerate

export const ALPHABET_NUMBER = '0123456789'
export const ALPHABET_LOWERCASE = 'abcdefghijklmnopqrstuvwxyz'
export const ALPHABET_UPPERCASE = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
export const ALPHABET_ALPHANUMERIC_LOWERCASE = [ALPHABET_NUMBER, ALPHABET_LOWERCASE].join('')
export const ALPHABET_ALPHANUMERIC_UPPERCASE = [ALPHABET_NUMBER, ALPHABET_UPPERCASE].join('')
export const ALPHABET_ALPHANUMERIC = [ALPHABET_NUMBER, ALPHABET_LOWERCASE, ALPHABET_UPPERCASE].join(
'',
)

/**
* Generate cryptographically-secure string id.
* Powered by `nanoid`.
*/
export function stringId (length = 16, alphabet = ALPHABET_ALPHANUMERIC_LOWERCASE): string {
return nanoidGenerate(alphabet, length)
}
@@ -283,9 +283,9 @@
integrity sha512-mV6T0IYqb0xL1UALPFplXYQmR0twnXG0M6jUswpquqT2sD12BOiCiLy3EvMp/Fy7s3DZElC4/aPjEjo2jeZpvw==

"@hapi/hoek@6.x.x":
version "6.2.1"
resolved "https://registry.yarnpkg.com/@hapi/hoek/-/hoek-6.2.1.tgz#d3a66329159af879bfdf0b0cff2229c43c5a3451"
integrity sha512-+ryw4GU9pjr1uT6lBuErHJg3NYqzwJTvZ75nKuJijEzpd00Uqi6oiawTGDDf5Hl0zWmI7qHfOtaqB0kpQZJQzA==
version "6.2.3"
resolved "https://registry.yarnpkg.com/@hapi/hoek/-/hoek-6.2.3.tgz#0abff7ac75d0c5388d2829c464b2aff74d473721"
integrity sha512-CtV9cp35+6Sfh6OfB+AYBozNIorZ6npNJjfO8InIyh/iFQI7uBW9bIApYoYf6TWq9w9BArecw2DDJf7oK+VlRw==

"@hapi/joi@^15.0.2":
version "15.0.3"
@@ -566,9 +566,9 @@
universal-user-agent "^2.1.0"

"@octokit/rest@^16.13.1":
version "16.25.6"
resolved "https://registry.yarnpkg.com/@octokit/rest/-/rest-16.25.6.tgz#6bc3a7da834d0c6f6076c384a89158b892dbeb2d"
integrity sha512-pUM6n8OKo2lyr8zxdgkeWrqRZvnKhor420KrbcrhD7L2tJJ9VEjE/yVGySfswbMjOKJMcIGWDHfA7b3JuuueEw==
version "16.26.0"
resolved "https://registry.yarnpkg.com/@octokit/rest/-/rest-16.26.0.tgz#5c12b28763219045e1c9a15182e8dfaed10004e8"
integrity sha512-NBpzre44ZAQWZhlH+zUYTgqI0pHN+c9rNj4d+pCydGEiKTGc1HKmoTghEUyr9GxazDyoAvmpx9nL0I7QS1Olvg==
dependencies:
"@octokit/request" "^4.0.1"
"@octokit/request-error" "^1.0.2"
@@ -2403,9 +2403,9 @@ fast-deep-equal@^2.0.1:
integrity sha1-ewUhjd+WZ79/Nwv3/bLLFf3Qqkk=

fast-glob@^2.2.6:
version "2.2.6"
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-2.2.6.tgz#a5d5b697ec8deda468d85a74035290a025a95295"
integrity sha512-0BvMaZc1k9F+MeWWMe8pL6YltFzZYcJsYU7D4JyDA6PAczaXvxqQQ/z+mDF7/4Mw01DeUc+i3CTKajnkANkV4w==
version "2.2.7"
resolved "https://registry.yarnpkg.com/fast-glob/-/fast-glob-2.2.7.tgz#6953857c3afa475fff92ee6015d52da70a4cd39d"
integrity sha512-g1KuQwHOZAmOZMuBtHdxDtju+T2RT8jgCC9aANsbpdiDDTSnjgfuVsIBNKbUeJI3oKMRExcfNDtJl4OhbffMsw==
dependencies:
"@mrmlnc/readdir-enhanced" "^2.2.1"
"@nodelib/fs.stat" "^1.1.2"
@@ -4845,6 +4845,11 @@ nan@^2.0.5, nan@^2.12.1:
resolved "https://registry.yarnpkg.com/nan/-/nan-2.14.0.tgz#7818f722027b2459a86f0295d434d1fc2336c52c"
integrity sha512-INOFj37C7k3AfaNTtX8RhsTw7qRy7eLET14cROi9+5HAVbbHuIWUHEauBv5qT4Av2tWasiTY1Jw6puUNqRJXQg==

nanoid@^2.0.2:
version "2.0.2"
resolved "https://registry.yarnpkg.com/nanoid/-/nanoid-2.0.2.tgz#2163edc84828cd42f9b8e4578979a4b5ffc1bb18"
integrity sha512-X4yQ8VHoFvHcykGunT2Jxrsm1c4vH5UKtau7LLJYXO1istCRE3jD8JxDyGCzN+h7dpWBCvWaSYgloRuphKRqUQ==

nanomatch@^1.2.9:
version "1.2.13"
resolved "https://registry.yarnpkg.com/nanomatch/-/nanomatch-1.2.13.tgz#b87a8aa4fc0de8fe6be88895b38983ff265bd119"

0 comments on commit 8110084

Please sign in to comment.
You can’t perform that action at this time.