-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathxor.js
More file actions
69 lines (54 loc) · 1.58 KB
/
xor.js
File metadata and controls
69 lines (54 loc) · 1.58 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
function XorEncoder() {
this.key = '';
this.iv = '';
};
XorEncoder.prototype.validateKeySize = function(){
};
XorEncoder.prototype.open = function(key, iv){
this.key = key;
this.iv = iv;
};
XorEncoder.prototype.encrypt = function(buffer) {
var encoding = 'binary'
var value = buffer.toString(encoding);
String.prototype.repeat = function(n) {
var str = '';
for (var i = 0; i < n; i++) {
str += this;
}
return str;
};
function repeatString(toExtend, base) {
var timesToRepeat = Math.ceil(base.length / toExtend.length);
return toExtend.repeat(timesToRepeat);
};
function charArray(value) {
var map = Array.prototype.map;
var chars = map.call(value, function(x) {
return x.charCodeAt(0);
});
return chars;
};
function joinCharCodes(a) {
// var finalstring = "";
// a.map(function(x){
// finalstring = finalstring + String.fromCharCode(x);
// })
var b = new Buffer(a,encoding);
return b;
}
function xorArrays(a, b){
var result = [];
for (var i = 0; i < a.length; i++) {
result[i] = a[i] ^ b[i];
}
return result;
};
var valueChars = charArray(value);
var repeatedPassChars = charArray(repeatString(this.key, value));
var repeatedIVChars = charArray(repeatString(this.iv, value));
var pre = xorArrays(valueChars, repeatedIVChars);
var post = xorArrays(pre, repeatedPassChars);
return joinCharCodes(post);
}
module.exports = XorEncoder;