-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a7a5f4d
commit e822c95
Showing
4 changed files
with
105 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import crypto from 'crypto' | ||
import * as dotenv from 'dotenv' | ||
import { NextFunction } from 'express' | ||
import { BadRequest } from '../../error/CustomError' | ||
import Log from '../log/Log' | ||
dotenv.config({ path: '.env' }) | ||
|
||
export default class Cipher { | ||
private static readonly _algorithm = 'aes-256-cbc' | ||
private static readonly _key = process.env.CIPHER_KEY as string | ||
private static readonly _iv = crypto.randomBytes(16) | ||
|
||
public static readonly encode = (input: string, next: NextFunction) => { | ||
try { | ||
const cipher = crypto.createCipheriv(this._algorithm, this._key, this._iv) | ||
let encrypted = cipher.update(input, 'utf8', 'hex') | ||
|
||
encrypted += cipher.final('hex') | ||
|
||
return `${this._iv.toString('hex')}:${encrypted}` | ||
} catch (error) { | ||
next(error) | ||
next() | ||
} | ||
} | ||
|
||
public static readonly decode = (input: string, next: NextFunction) => { | ||
try { | ||
const [ivHex, encrypted] = input.split(':') | ||
const iv = Buffer.from(ivHex, 'hex') | ||
|
||
const decipher = crypto.createDecipheriv(this._algorithm, this._key, iv) | ||
|
||
let decrypted = decipher.update(encrypted, 'hex', 'utf8') | ||
|
||
decrypted += decipher.final('utf8') | ||
|
||
return decrypted | ||
} catch (error) { | ||
Log.error('error', 'Cipher :: Decode', error) | ||
next(new BadRequest('Invalid token')) | ||
next() | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { NextFunction } from 'express' | ||
import { BadRequest } from 'src/error/CustomError' | ||
import Cipher from 'src/util/security/Cipher' | ||
|
||
describe('Cipher', () =>{ | ||
let next: NextFunction | ||
|
||
beforeEach(() => { | ||
next = jest.fn() | ||
}) | ||
|
||
afterEach(() => { | ||
jest.resetAllMocks() | ||
}) | ||
|
||
const sample = 'user-name_12+alias@example.com' | ||
|
||
it('should encode and decode', () => { | ||
const encoded = Cipher.encode(sample, next) | ||
console.log(encoded) | ||
const decoded = Cipher.decode(encoded as string, next) | ||
|
||
expect(decoded).toEqual(sample) | ||
}) | ||
|
||
it('should call next with a BadRequest', () => { | ||
Cipher.decode('string', next) | ||
|
||
expect(next).toHaveBeenCalledWith(new BadRequest('Invalid token')) | ||
}) | ||
|
||
it('should call next with an error', () => { | ||
Cipher.encode(undefined as unknown as string, next) | ||
|
||
expect(next).toHaveBeenCalled() | ||
expect(next).toHaveBeenCalledTimes(2) | ||
}) | ||
}) |