95 lines
2.7 KiB
TypeScript
95 lines
2.7 KiB
TypeScript
import { Injectable } from '@angular/core';
|
|
import CryptoJS from 'crypto-js';
|
|
import crypto from 'crypto';
|
|
import moment from 'moment';
|
|
|
|
import { CipherUtil } from '../utils/CipherUtil';
|
|
|
|
@Injectable({
|
|
providedIn: 'root'
|
|
})
|
|
export class DaesangCipherService {
|
|
constructor() {}
|
|
|
|
encrypt(pvUserKey: string, pvSource: string, isBase64: string): string {
|
|
const cipherKeyDTD = CipherUtil.makeCipherKey('AES', pvUserKey);
|
|
|
|
const cipher = crypto.createCipheriv(
|
|
'aes-128-cbc',
|
|
cipherKeyDTD.key,
|
|
cipherKeyDTD.iv
|
|
);
|
|
cipher.setAutoPadding(true);
|
|
|
|
let outputEncoding: 'base64' | 'hex';
|
|
|
|
if ('Y' === isBase64) {
|
|
outputEncoding = 'base64';
|
|
} else {
|
|
outputEncoding = 'hex';
|
|
}
|
|
|
|
const cipherChunks = [];
|
|
cipherChunks.push(cipher.update(pvSource, 'utf8', outputEncoding));
|
|
cipherChunks.push(cipher.final(outputEncoding));
|
|
|
|
return cipherChunks.join('');
|
|
}
|
|
|
|
encryptForWebLink(pvSource: string): string {
|
|
return CryptoJS.SHA1(pvSource)
|
|
.toString()
|
|
.toUpperCase();
|
|
}
|
|
|
|
/**
|
|
* Sap of Weblink
|
|
* @description
|
|
* const enc = this.daesangCipherService.encryptForSapErp('aes256-daesang-key!!',12345678);
|
|
* console.log('enc', enc);
|
|
* const dec = this.daesangCipherService.decryptForSapErp('aes256-daesang-key!!',enc);
|
|
* console.log('dec', dec);
|
|
*/
|
|
encryptForSapErp(pvUserKey: string, employeeNum: string): string {
|
|
// const txt = '20200221090321_asdfghjk'; // 1QgLAiLqJ6Uo6bE4Qk1o3Yd6mfqxXSnmqXX%2FXLL7DoA%3D
|
|
// const txt = '20200221101444_asdfghjk'; // Lz1TIdGTQQMui%2BBHMdj8fatYYhXbwJEL%2BJ91C7jUWEs%3D
|
|
const str =
|
|
moment().format('YYYYMMDDHHmmss') + '_' + employeeNum.toUpperCase();
|
|
const secretKeyToByteArray: Buffer = Buffer.from(pvUserKey, 'utf8').slice(
|
|
0,
|
|
16
|
|
);
|
|
const ivParamenter: Buffer = Buffer.from(pvUserKey.slice(0, 16));
|
|
const cipher: crypto.Cipher = crypto.createCipheriv(
|
|
'aes-128-cbc',
|
|
secretKeyToByteArray,
|
|
ivParamenter
|
|
);
|
|
let encryptedValue: string = cipher.update(str, 'utf8', 'base64');
|
|
encryptedValue += cipher.final('base64');
|
|
|
|
return encodeURIComponent(encryptedValue);
|
|
}
|
|
|
|
decryptForSapErp(pvUserKey: string, encryptedValue: string): string {
|
|
const secretKeyToByteArray: Buffer = Buffer.from(pvUserKey, 'utf8').slice(
|
|
0,
|
|
16
|
|
);
|
|
const ivParamenter: Buffer = Buffer.from(pvUserKey.slice(0, 16));
|
|
const cipher: crypto.Decipher = crypto.createDecipheriv(
|
|
'aes-128-cbc',
|
|
secretKeyToByteArray,
|
|
ivParamenter
|
|
);
|
|
let decryptedValue: string = cipher.update(
|
|
decodeURIComponent(encryptedValue),
|
|
'base64',
|
|
'utf8'
|
|
);
|
|
decryptedValue += cipher.final('utf8');
|
|
|
|
return decryptedValue;
|
|
}
|
|
}
|