CipherObject.js
3.82 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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// Generated by CoffeeScript 1.10.0
(function() {
var BufSlicer, CipherObject, E, buf2str, semver, str2buf;
semver = require('semver');
E = require('./constants');
buf2str = function(buf, enc) {
if (enc == null) {
enc = 'base64';
}
if (!Buffer.isEncoding(enc)) {
throw new Error("CipherObject: buf2str Buffer.isEncoding('" + enc + "') returned false.");
}
return ":" + enc + ":" + (buf.toString(enc));
};
str2buf = function(str) {
var enc, m;
m = str.match(/^:(.*):/);
if (m == null) {
throw new Error("CipherObject: str2buf run on string but no :encoding: found at start");
}
enc = m[1];
if (!Buffer.isEncoding(enc)) {
throw new Error("CipherObject: str2buf run on string with encoding :" + enc + ": but Buffer.isEncoding('" + enc + "') returned false.");
}
str = str.slice(m[0].length);
return new Buffer(str, enc);
};
BufSlicer = (function() {
function BufSlicer(buf1, beg, end) {
this.buf = buf1;
this.beg = beg != null ? beg : 0;
this.end = end != null ? end : 0;
}
BufSlicer.prototype.slice = function(len) {
this.beg = this.end;
this.end = len != null ? this.end + len : this.buf.length;
return this.buf.slice(this.beg, this.end);
};
return BufSlicer;
})();
CipherObject = (function() {
function CipherObject(o) {
var p;
switch (false) {
case !(o instanceof Buffer):
return this.fromBuffer(o);
case typeof o !== 'string':
return this.fromString(o);
case typeof o !== 'object':
for (p in o) {
if (typeof o[p] !== 'function') {
this[p] = o[p];
}
}
}
}
CipherObject.prototype.toString = function() {
return JSON.stringify({
yael_version: this.yael_version,
cipherfile: buf2str(this.cipherfile),
iv: buf2str(this.iv),
salt: buf2str(this.salt),
authtag: buf2str(this.authtag),
return_type: this.return_type,
details: this.details
});
};
CipherObject.prototype.fromString = function(o) {
var error, json, p;
try {
json = JSON.parse(o);
} catch (error) {
throw new Error("CipherObject.fromString: Invalid JSON");
}
for (p in json) {
this[p] = json[p];
}
this.cipherfile = str2buf(this.cipherfile);
this.iv = str2buf(this.iv);
this.salt = str2buf(this.salt);
this.authtag = str2buf(this.authtag);
return this;
};
CipherObject.prototype.toBuffer = function() {
var t, v;
v = semver.parse(this.yael_version);
t = this.return_type[0];
return Buffer.concat([new Buffer([v.major, v.minor, v.patch]), new Buffer(t), this.salt, this.iv, this.authtag, this.cipherfile]);
};
CipherObject.prototype.fromBuffer = function(o) {
var buf, major, minor, patch, ref, return_type;
buf = new BufSlicer(o);
ref = buf.slice(3), major = ref[0], minor = ref[1], patch = ref[2];
this.yael_version = major + '.' + minor + '.' + patch;
return_type = buf.slice(1);
this.return_type = (function() {
switch (return_type.toString()) {
case 'S':
return 'String';
case 'B':
return 'Buffer';
}
})();
this.salt = buf.slice(E.SALT_LENGTH);
this.iv = buf.slice(E.IV_LENGTH);
this.authtag = buf.slice(E.AUTHTAG_LENGTH);
this.cipherfile = buf.slice();
this.details = {
CIPHER_ALGORITHM: E.CIPHER_ALGORITHM,
SALT_LENGTH: E.SALT_LENGTH,
IV_LENGTH: E.IV_LENGTH,
KEY_LENGTH: E.KEY_LENGTH,
HASH_ALGORITHM: E.HASH_ALGORITHM,
ITERATIONS: E.ITERATIONS
};
return this;
};
return CipherObject;
})();
module.exports = CipherObject;
}).call(this);