-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwheel.js
28 lines (21 loc) · 1007 Bytes
/
wheel.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
var Wheel = function(multiplier, previousMultiplier) {
// for explanation of private, public and priviledged
// see http://javascript.crockford.com/private.html
var wheelLetters = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.,?!'\" ";
var self = this;
this.position = 0; // public
this.positionOf = function(ch) {
return wheelLetters.indexOf(ch);
}
this.encrypt = function(ch, previousCharPosition) { // priviledged method
return wheelLetters.charAt(offsetForEncryptionWithWrap(self.positionOf(ch),self.position, previousCharPosition));
};
function offsetForEncryptionWithWrap(indexOfPlainTextChar, pos, previousCharPosition) {
var newPosition = indexOfPlainTextChar + (pos * multiplier) + (previousCharPosition * previousMultiplier);
var wrappedPosition = (newPosition + wheelLetters.length) % wheelLetters.length;
return wrappedPosition;
}
}
exports.create = function(multiplier, previousMultiplier) {
return new Wheel(multiplier, previousMultiplier);
}