Skip to content

Instantly share code, notes, and snippets.

@austbot
Created February 19, 2018 20:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save austbot/cfb25548e6295ce3d80ca02f8758292d to your computer and use it in GitHub Desktop.
Save austbot/cfb25548e6295ce3d80ca02f8758292d to your computer and use it in GitHub Desktop.
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