-
Notifications
You must be signed in to change notification settings - Fork 12
/
pidcrypt.js
215 lines (207 loc) · 7.76 KB
/
pidcrypt.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*!Copyright (c) 2009-2015 pidder, https://www.pidder.de */
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
// 02111-1307 USA or check at http://www.gnu.org/licenses/gpl.html
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* pidCrypt is pidders JavaScript Crypto Library - https://www.pidder.de/pidcrypt
* Version 0.06, 01/2015
*
* pidCrypt is a combination of different JavaScript functions for client side
* encryption technologies with enhancements for openssl compatibility cast into
* a modular class concept.
*
* Client side encryption is a must have for developing host proof applications:
* There must be no knowledge of the clear text data at the server side, all
* data is enrycpted prior to being submitted to the server.
* Client side encryption is mandatory for protecting the privacy of the users.
* "Dont't trust us, check our source code!"
*
* "As a cryptography and computer security expert, I have never understood
* the current fuss about the open source software movement. In the
* cryptography world, we consider open source necessary for good security;
* we have for decades. Public security is always more secure than proprietary
* security. It's true for cryptographic algorithms, security protocols, and
* security source code. For us, open source isn't just a business model;
* it's smart engineering practice."
* Bruce Schneier, Crypto-Gram 1999/09/15
* copied form keepassx site - keepassx is a cross plattform password manager
*
* pidCrypt comes with modules under different licenses and copyright terms.
* Make sure that you read and respect the individual module license conditions
* before using it.
*
* The pidCrypt base library contains:
* 1. pidcrypt.js
* class pidCrypt: the base class of the library
* 2. pidcrypt_util.js
* base64 en-/decoding as new methods of the JavaScript String class
* UTF8 en-/decoding as new methods of the JavaScript String class
* String/HexString conversions as new methods of the JavaScript String class
*
* The pidCrypt modules and the original authors (see files for detailed
* copyright and license terms) are:
*
* - md5.js: MD5 (Message-Digest Algorithm), www.webtoolkit.info
* - aes_core.js: AES (Advanced Encryption Standard ) Core algorithm, B. Poettering
* - aes-ctr.js: AES CTR (Counter) Mode, Chis Veness
* - aes-cbc.js: AES CBC (Cipher Block Chaining) Mode, pidder
* - jsbn.js: BigInteger for JavaScript, Tom Wu
* - prng.js: PRNG (Pseudo-Random Number Generator), Tom Wu
* - rng.js: Random Numbers, Tom Wu
* - rsa.js: RSA (Rivest, Shamir, Adleman Algorithm), Tom Wu
* - oids.js: oids (Object Identifiers found in ASN.1), Peter Gutmann
* - asn1.js: ASN1 (Abstract Syntax Notation One) parser, Lapo Luchini
* - sha256.js SHA-256 hashing, Angel Marin
* - sha2.js: SHA-384 and SHA-512 hashing, Brian Turek
* - seedrandom.js seeded random number generator, David Bau
*
* IMPORTANT:
* Please report any bugs at https://sourceforge.net/projects/pidcrypt/
* Vist https://www.pidder.de/pidcrypt for online demo and documentation
*/
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
var pidCryptUtil = require('./pidcrypt_util.js');
function pidCrypt(){
function getRandomBytes(len){
/* for better randomness make sure you incorporate and initiate seedrandom.js
* before using getRandomBytes() or any call of Math.random()
*/
if(!len) len = 8;
var bytes = new Array(len);
var field = [];
for(var i=0;i<256;i++) field[i] = i;
for(i=0;i<bytes.length;i++)
bytes[i] = field[Math.floor(Math.random()*field.length)];
return bytes
}
this.setDefaults = function(){
this.params.nBits = 256;
//salt should always be a Hex String e.g. AD0E76FF6535AD...
this.params.salt = pidCryptUtil.convertToHex(pidCryptUtil.byteArray2String(getRandomBytes(8)));
this.params.blockSize = 16;
this.params.UTF8 = true;
this.params.A0_PAD = true;
}
this.debug = true;
this.params = {};
//setting default values for params
this.params.dataIn = '';
this.params.dataOut = '';
this.params.decryptIn = '';
this.params.decryptOut = '';
this.params.encryptIn = '';
this.params.encryptOut = '';
//key should always be a Hex String e.g. AD0E76FF6535AD...
this.params.key = '';
//iv should always be a Hex String e.g. AD0E76FF6535AD...
this.params.iv = '';
this.params.clear = true;
this.setDefaults();
this.errors = '';
this.warnings = '';
this.infos = '';
this.debugMsg = '';
//set and get methods for base class
this.setParams = function(pObj){
if(!pObj) pObj = {};
for(var p in pObj)
this.params[p] = pObj[p];
}
this.getParams = function(){
return this.params;
}
this.getParam = function(p){
return this.params[p] || '';
}
this.clearParams = function(){
this.params= {};
}
this.getNBits = function(){
return this.params.nBits;
}
this.getOutput = function(){
return this.params.dataOut;
}
this.setError = function(str){
this.error = str;
}
this.appendError = function(str){
this.errors += str;
return '';
}
this.getErrors = function(){
return this.errors;
}
this.isError = function(){
if(this.errors.length>0)
return true;
return false
}
this.appendInfo = function(str){
this.infos += str;
return '';
}
this.getInfos = function()
{
return this.infos;
}
this.setDebug = function(flag){
this.debug = flag;
}
this.appendDebug = function(str)
{
this.debugMsg += str;
return '';
}
this.isDebug = function(){
return this.debug;
}
this.getAllMessages = function(options){
var defaults = {lf:'\n',
clr_mes: false,
verbose: 15//verbose level bits = 1111
};
if(!options) options = defaults;
for(var d in defaults)
if(typeof(options[d]) == 'undefined') options[d] = defaults[d];
var mes = '';
var tmp = '';
for(var p in this.params){
switch(p){
case 'encryptOut':
tmp = this.params[p].toString().toByteArray();
tmp = tmp.join().fragment(64, options.lf)
break;
case 'key':
case 'iv':
tmp = this.params[p].formatHex(48);
break;
default:
tmp = this.params[p].toString().fragment(64, options.lf);
}
mes += '<p><b>'+p+'</b>:<pre>' + tmp + '</pre></p>';
}
if(this.debug) mes += 'debug: ' + this.debug + options.lf;
if(this.errors.length>0 && ((options.verbose & 1) == 1)) mes += 'Errors:' + options.lf + this.errors + options.lf;
if(this.warnings.length>0 && ((options.verbose & 2) == 2)) mes += 'Warnings:' +options.lf + this.warnings + options.lf;
if(this.infos.length>0 && ((options.verbose & 4) == 4)) mes += 'Infos:' +options.lf+ this.infos + options.lf;
if(this.debug && ((options.verbose & 8) == 8)) mes += 'Debug messages:' +options.lf+ this.debugMsg + options.lf;
if(options.clr_mes)
this.errors = this.infos = this.warnings = this.debug = '';
return mes;
}
this.getRandomBytes = function(len){
return getRandomBytes(len);
}
//TODO warnings
}
module.exports = pidCrypt;