본문 바로가기
nest.js

SHA256 암호화 알고리즘으로 Text 암호화 예제

by Hwoarang757 2024. 9. 30.

(1) 암호화 역할 함수를 작성 하였습니다.

//test.encrypt.helper.ts

export class TestEncryptHelper {
  static async encrypt(password: string): Promise<string> {
    const { createHash } = require('crypto');
    return createHash('sha256').update(`${password}[SaltValue]`).digest('hex');
  }
}

 

(2) 단위 테스트를 진행해보기 위하여 작성 하였습니다.

//test.encrypt.helper.spec.ts

import { TestEncryptHelper } from './test.encrypt.helper'

 describe('encryptTest' , () => {

     it('Text encrypt', async () => {
        await TestEncryptHelper.encrypt("1111").then((a) => {
            expect(a).toEqual("499327aa0b416319b8f52ec8c8f9f397dcdacc5839bce7dd6080844e59e42bf9");
        });

    });

  });

 

(3) 테스트 결과 입니다. 

$ npm run test

> test_api@0.0.1 test
> jest

 PASS  src/common/helpers/test.encrypt.helper.spec.ts (14.264 s)
  encryptTest
    √ Text encrypt (9 ms)
                                                                                                                        
Test Suites: 1 passed, 1 total
Tests:       1 passed, 1 total
Snapshots:   0 total
Time:        14.857 s
Ran all test suites.