does not pass tests yet

This commit is contained in:
Bart Butler 2015-04-20 14:51:56 -07:00 committed by Tankred Hase
parent 1c6e777a3d
commit 5711028449
12 changed files with 1569 additions and 1077 deletions

View File

@ -3,7 +3,8 @@
* @module crypto/hash
*/
var sha = require('./sha.js'),
forge_sha256 = require('./forge_sha256.js');
forge_sha256 = require('./forge_sha256.js'),
util = require('../../util.js');
module.exports = {
/** @see module:crypto/hash/md5 */
@ -24,8 +25,8 @@ module.exports = {
/**
* Create a hash on the specified data using the specified algorithm
* @param {module:enums.hash} algo Hash algorithm type (see {@link http://tools.ietf.org/html/rfc4880#section-9.4|RFC 4880 9.4})
* @param {String} data Data to be hashed
* @return {String} hash value
* @param {Uint8Array} data Data to be hashed
* @return {Uint8Array} hash value
*/
digest: function(algo, data) {
switch (algo) {
@ -40,9 +41,10 @@ module.exports = {
return this.ripemd(data);
case 8:
// - SHA256 [FIPS180]
//return this.sha256(data);
var sha256 = forge_sha256.create();
sha256.update(data);
return sha256.digest().getBytes();
sha256.update(util.Uint8Array2str(data));
return util.str2Uint8Array(sha256.digest().getBytes());
case 9:
// - SHA384 [FIPS180]
return this.sha384(data);

View File

@ -24,8 +24,8 @@ var util = require('../../util.js');
* @param {String} entree string to hash
*/
module.exports = function (entree) {
var hex = md5(entree);
var bin = util.hex2bin(hex);
var hex = md5(util.Uint8Array2str(entree));
var bin = util.str2Uint8Array(util.hex2bin(hex));
return bin;
};

View File

@ -20,10 +20,16 @@
/* Modified by Recurity Labs GmbH
*/
/* Modified by ProtonTech AG
*/
/**
* @requires util
* @module crypto/hash/ripe-md
*/
var util = require('../../util.js');
var RMDsize = 160;
var X = [];
@ -284,14 +290,14 @@ function RMD(message) {
function RMDstring(message) {
var hashcode = RMD(message);
var hashcode = RMD(util.Uint8Array2str(message));
var retString = "";
for (var i = 0; i < RMDsize / 8; i++) {
retString += String.fromCharCode(hashcode[i]);
}
return retString;
return util.str2Uint8Array(retString);
}
module.exports = RMDstring;

File diff suppressed because it is too large Load Diff

View File

@ -135,7 +135,7 @@ module.exports = {
encode: function(algo, M, emLen) {
var i;
// Apply the hash function to the message M to produce a hash value H
var H = hash.digest(algo, M);
var H = util.Uint8Array2str(hash.digest(algo, util.str2Uint8Array(M)));
if (H.length !== hash.getHashByteLength(algo)) {
throw new Error('Invalid hash length');
}

View File

@ -88,7 +88,7 @@ module.exports = {
var n = keyIntegers[0].toBigInteger();
m = pkcs1.emsa.encode(hash_algo,
data, keyIntegers[0].byteLength());
return rsa.sign(m, d, n).toMPI();
return util.str2Uint8Array(rsa.sign(m, d, n).toMPI());
case 17:
// DSA (Digital Signature Algorithm) [FIPS186] [HAC]
@ -102,7 +102,7 @@ module.exports = {
m = data;
var result = dsa.sign(hash_algo, m, g, p, q, x);
return result[0].toString() + result[1].toString();
return util.str2Uint8Array(result[0].toString() + result[1].toString());
case 16:
// Elgamal (Encrypt-Only) [ELGAMAL] [HAC]
throw new Error('Signing with Elgamal is not defined in the OpenPGP standard.');

View File

@ -191,13 +191,13 @@ PublicKey.prototype.getFingerprint = function () {
var toHash = '';
if (this.version == 4) {
toHash = this.writeOld();
this.fingerprint = crypto.hash.sha1(util.Uint8Array2str(toHash));
this.fingerprint = util.Uint8Array2str(crypto.hash.sha1(toHash));
} else if (this.version == 3) {
var mpicount = crypto.getPublicMpiCount(this.algorithm);
for (var i = 0; i < mpicount; i++) {
toHash += this.mpi[i].toBytes();
}
this.fingerprint = crypto.hash.md5(toHash);
this.fingerprint = util.Uint8Array2str(crypto.hash.md5(util.str2Uint8Array(toHash)));
}
this.fingerprint = util.hexstrdump(this.fingerprint);
return this.fingerprint;

View File

@ -68,7 +68,7 @@ function get_hash_fn(hash) {
return crypto.hash.sha1;
else
return function(c) {
return util.Uint8Array2str(util.writeNumber(util.calc_checksum(util.str2Uint8Array(c)), 2));
return util.writeNumber(util.calc_checksum(c), 2);
};
}
@ -81,7 +81,11 @@ function parse_cleartext_mpi(hash_algorithm, cleartext, algorithm) {
var hashtext = util.Uint8Array2str(cleartext.subarray(cleartext.length - hashlen, cleartext.length));
cleartext = cleartext.subarray(0, cleartext.length - hashlen);
var hash = hashfn(util.Uint8Array2str(cleartext));
var hash = util.Uint8Array2str(hashfn(cleartext));
console.log(hash);
console.log(hashtext);
console.log(hash_algorithm);
if (hash != hashtext)
return new Error("Hash mismatch.");
@ -109,9 +113,9 @@ function write_cleartext_mpi(hash_algorithm, algorithm, mpi) {
var bytes = util.concatUint8Array(arr);
var hash = get_hash_fn(hash_algorithm)(util.Uint8Array2str(bytes));
var hash = get_hash_fn(hash_algorithm)(bytes);
return util.concatUint8Array([bytes, util.str2Uint8Array(hash)]);
return util.concatUint8Array([bytes, hash]);
}
@ -220,16 +224,6 @@ SecretKey.prototype.decrypt = function (passphrase) {
symmetric,
key;
if(!Uint8Array.prototype.isPrototypeOf(this.encrypted)) {
if(Uint8Array.prototype.isPrototypeOf(this.encrypted.message)) {
throw new Error('this.encrypted.message is a typed array!');
}
if(this.encrypted === null) {
throw new Error('this.encrypted is null!');
}
throw new Error(Object.prototype.toString.call(this.encrypted));
}
var s2k_usage = this.encrypted[i++];
// - [Optional] If string-to-key usage octet was 255 or 254, a one-
@ -248,7 +242,7 @@ SecretKey.prototype.decrypt = function (passphrase) {
} else {
symmetric = s2k_usage;
symmetric = enums.read(enums.symmetric, symmetric);
key = util.str2Uint8Array(crypto.hash.md5(passphrase));
key = crypto.hash.md5(passphrase);
}

View File

@ -224,14 +224,14 @@ Signature.prototype.sign = function (key, data) {
var trailer = this.calculateTrailer();
var toHash = util.Uint8Array2str(util.concatUint8Array([this.toSign(signatureType, data), this.signatureData, trailer]));
var toHash = util.concatUint8Array([this.toSign(signatureType, data), this.signatureData, trailer]);
var hash = crypto.hash.digest(hashAlgorithm, toHash);
this.signedHashValue = util.str2Uint8Array(hash.substr(0, 2));
this.signedHashValue = hash.subarray(0, 2);
this.signature = util.str2Uint8Array(crypto.signature.sign(hashAlgorithm,
publicKeyAlgorithm, key.mpi, util.str2Uint8Array(toHash)));
this.signature = crypto.signature.sign(hashAlgorithm,
publicKeyAlgorithm, key.mpi, toHash);
};
/**
@ -287,7 +287,7 @@ Signature.prototype.write_all_sub_packets = function () {
// 2 octets of value length
bytes.push(util.writeNumber(value.length, 2));
bytes.push(util.str2Uint8Array(name + value));
bytes = concatUint8Array(bytes);
bytes = util.concatUint8Array(bytes);
arr.push(write_sub_packet(sub.notation_data, bytes));
}
}

View File

@ -87,7 +87,7 @@ SymEncryptedIntegrityProtected.prototype.encrypt = function (sessionKeyAlgorithm
// This could probably be cleaned up to use less memory
var tohash = util.concatUint8Array([bytes, mdc]);
var hash = util.str2Uint8Array(crypto.hash.sha1(util.Uint8Array2str(util.concatUint8Array([prefix, tohash]))));
var hash = crypto.hash.sha1(util.concatUint8Array([prefix, tohash]));
tohash = util.concatUint8Array([tohash, hash]);
@ -111,7 +111,7 @@ SymEncryptedIntegrityProtected.prototype.decrypt = function (sessionKeyAlgorithm
// there must be a modification detection code packet as the
// last packet and everything gets hashed except the hash itself
this.hash = crypto.hash.sha1(util.Uint8Array2str(util.concatUint8Array([crypto.cfb.mdc(sessionKeyAlgorithm, key, this.encrypted),
this.hash = util.Uint8Array2str(crypto.hash.sha1(util.concatUint8Array([crypto.cfb.mdc(sessionKeyAlgorithm, key, this.encrypted),
decrypted.subarray(0, decrypted.length - 20)])));
var mdc = util.Uint8Array2str(decrypted.subarray(decrypted.length - 20, decrypted.length));

View File

@ -142,33 +142,33 @@ S2K.prototype.write = function () {
* hashAlgorithm hash length
*/
S2K.prototype.produce_key = function (passphrase, numBytes) {
passphrase = util.encode_utf8(passphrase);
passphrase = util.str2Uint8Array(util.encode_utf8(passphrase));
function round(prefix, s2k) {
var algorithm = enums.write(enums.hash, s2k.algorithm);
switch (s2k.type) {
case 'simple':
return util.str2Uint8Array(crypto.hash.digest(algorithm, prefix + passphrase));
return crypto.hash.digest(algorithm, util.concatUint8Array([prefix,passphrase]));
case 'salted':
return util.str2Uint8Array(crypto.hash.digest(algorithm,
prefix + util.Uint8Array2str(s2k.salt) + passphrase));
return crypto.hash.digest(algorithm,
util.concatUint8Array([prefix, s2k.salt, passphrase]));
case 'iterated':
var isp = [],
count = s2k.get_count(),
data = util.Uint8Array2str(s2k.salt) + passphrase;
data = util.concatUint8Array([s2k.salt,passphrase]);
while (isp.length * data.length < count)
isp.push(data);
isp = isp.join('');
isp = util.concatUint8Array(isp);
if (isp.length > count)
isp = isp.substr(0, count);
isp = isp.subarray(0, count);
return util.str2Uint8Array(crypto.hash.digest(algorithm, prefix + isp));
return crypto.hash.digest(algorithm, util.concatUint8Array([prefix,isp]));
case 'gnu':
throw new Error("GNU s2k type not supported.");
@ -179,14 +179,19 @@ S2K.prototype.produce_key = function (passphrase, numBytes) {
}
var arr = [],
i = 0,
rlength = 0,
prefix = '';
prefix = new Uint8Array(numBytes);
for(var i = 0; i<numBytes; i++) {
prefix[i] = 0;
}
while (rlength <= numBytes) {
var result = round(prefix, this);
var result = round(prefix.subarray(0,i), this);
arr.push(result);
rlength += result.length;
prefix += String.fromCharCode(0);
i++;
}
return util.concatUint8Array(arr).subarray(0, numBytes);

View File

@ -190,7 +190,7 @@ module.exports = {
// Uncomment for debugging
if(!(typeof str === 'string') && !String.prototype.isPrototypeOf(str)) {
throw new Error('Data must be in the form of a string');
throw new Error('str2Uint8Array: Data must be in the form of a string');
}
var result = new Uint8Array(str.length);
@ -211,7 +211,7 @@ module.exports = {
// Uncomment for debugging
if(!Uint8Array.prototype.isPrototypeOf(bin)) {
throw new Error('Data must be in the form of a Uint8Array');
throw new Error('Uint8Array2str: Data must be in the form of a Uint8Array');
}
var result = [];
@ -234,7 +234,9 @@ module.exports = {
// Uncomment for debugging
if(!Uint8Array.prototype.isPrototypeOf(element)) {
throw new Error('Data must be in the form of a Uint8Array');
var err = new Error('here');
console.log(err.stack);
throw new Error('concatUint8Array: Data must be in the form of a Uint8Array');
}
totalLength += element.length;