Created
October 24, 2017 18:24
-
-
Save Romain-P/b84d35b3b3765f37f7ea21ab57744e9f to your computer and use it in GitHub Desktop.
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 {config} from "../app.config"; | |
| import {Buffer} from 'buffer/'; | |
| import * as crypto from "crypto-browserify"; | |
| export class RsaService { | |
| private publicKey: string; | |
| private enabled: boolean; | |
| constructor() { | |
| //BEP format required by crypto | |
| this.publicKey = "-----BEGIN PUBLIC KEY-----\n" + config.authentication.rsa.publicKey + "\n-----END PUBLIC KEY-----"; | |
| this.enabled = config.authentication.rsa.enabled; | |
| } | |
| isEnabled(): boolean { | |
| return this.enabled; | |
| } | |
| encrypt(plaintext: string): string { | |
| if (!this.enabled) | |
| return plaintext; | |
| let buffer = new Buffer(plaintext); | |
| let encrypted = crypto.publicEncrypt({ key: this.publicKey, padding: crypto.constants.RSA_PKCS1_PADDING}, buffer); | |
| return encrypted.toString('base64'); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment