Created
February 19, 2018 20:50
-
-
Save austbot/cfb25548e6295ce3d80ca02f8758292d to your computer and use it in GitHub Desktop.
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
import { Injectable, Inject } from '@angular/core'; | |
@Injectable() | |
export class SecretService { | |
constructor( @Inject('ENcRYPTI0n_k3y') private key: string) {} | |
//public api | |
encode(secret: string) { | |
return SecretService._encodeSecret(this.key, secret); | |
} | |
//private api | |
static _mapToKeyChar(key: string, char: str, index: number) { | |
const len = key.length; | |
const dist = index > len-1 ? index - len : len - index; | |
return key[dist]; | |
} | |
static _encodeSecret(key: string, secret: string) { | |
return secret | |
.split('') | |
.reverse() | |
.map((char, index, str) => { | |
return SecretService._mapToKeyChar(key, char, index); | |
}) | |
.join(''); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment