Created
July 6, 2020 17:03
-
-
Save allenhwkim/1a808441550c12742b12dd121dd55193 to your computer and use it in GitHub Desktop.
Angular: Encrypt/Decrypt
This file contains hidden or 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
| import { Pipe, PipeTransform } from '@angular/core'; | |
| import * as CryptoJS from 'crypto-js'; | |
| const key = 'TUc0emRqRXpkdw=='; // btoa(btoa(myKey)); | |
| @Pipe({name: 'encrypted'}) | |
| export class EncryptPipe implements PipeTransform { | |
| transform(value: string) { | |
| if (value) { | |
| return CryptoJS.AES.encrypt(value, key).toString(); | |
| } | |
| } | |
| } | |
| @Pipe({name: 'decrypted'}) | |
| export class DecryptPipe implements PipeTransform { | |
| transform(encrypted: string) { | |
| if (encrypted) { | |
| const decrypted = CryptoJS.AES.decrypt(encrypted, key); | |
| return decrypted.toString(CryptoJS.enc.Utf8); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment