Skip to content

Instantly share code, notes, and snippets.

@austbot
Last active February 19, 2018 21:30
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/90d318f78f972ab7b06b69e750223fb0 to your computer and use it in GitHub Desktop.
Save austbot/90d318f78f972ab7b06b69e750223fb0 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, private features: FeatureFlags) {}
//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('');
}
}
function createService(key, flags) {
const featureFlags = new FeatureFlags(flags) //imaging flags is an object {"feature1": false ...
return new SecretService(key, featureFlags);
}
describe('Secret Service', () => {
it('uses better encryption when its on', () => {
const svc = createService("key", {'more_entropy': true}); //Create a 'context'
const result = SecretService.prototype.encode.call(svc); //Test the function at that 'context'
expect(result).toBe(blahblhanblah);
});
...
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment