Skip to content

Instantly share code, notes, and snippets.

@dlaxar
Last active December 11, 2015 04:38
Show Gist options
  • Save dlaxar/4546213 to your computer and use it in GitHub Desktop.
Save dlaxar/4546213 to your computer and use it in GitHub Desktop.
MPCA #5
var str = "Wir berechnen einen ASCII Wert";
var max = 0, temp;
for(var i = 0; i < str.length; i++) {
temp = str.charCodeAt(i);
if(temp > max) {
max = temp
}
}
console.log('Der größte ASCII-Wert ist:', max, '(' + String.fromCharCode(max) + ')');
var str = "Wir berechnen eine Summe";
var sum = 0;
for(var i = 0; i < str.length; i++) {
sum += parseInt(str.charCodeAt(i));
}
console.log('Die ASCII-Summe beträgt:', sum);
var hash = require('./hash.js');
var strings = [
'Elements can be added',
'but not removed (though this can be addressed with a counting filter)',
'The requirement of designing k different',
'For a good hash function',
'It is often the case that all the keys are available'
];
var hashCache;
for(var k in strings) {
console.log('Processing strings[' + k + ']:', strings[k]);
console.log('Hash value is:', hashCache = hash(strings[k], 32, 4));
console.log('Injective value is:', parseInt(hashCache)%255+1);
console.log('----')
}
var hash = require('./hash.js');
var string = 'A.block.quotation';
console.log('Processing ', string);
console.log('Hash value is:', hash(string, 32, 4));
var hash = require('./hash.js');
var str = 'Typesetting requires the prior process of designing a font and storing it in some manner. Typesetting is the retrieval of the stored letters (called sorts in mechanical systems and glyphs in digital systems) and the ordering of them according to a language\'s orthography for visual display';
console.log('The hash value is:', hash(str));
var filesPath = './files/';
var count = 4096;
var hash = require('./hash.js');
var fs = require('fs');
global.maxFilesInFlight = 1000; // Set this value to some number safeish for your system
var origRead = fs.readFile;
var origWrite = fs.writeFile;
var activeCount = 0;
var pending = [];
var wrapCallback = function(cb){
return function(){
activeCount--;
cb.apply(this,Array.prototype.slice.call(arguments));
if (activeCount < global.maxFilesInFlight && pending.length){
//console.log("Processing Pending read/write");
pending.shift()();
}
};
};
fs.readFile = function(){
var args = Array.prototype.slice.call(arguments);
if (activeCount < global.maxFilesInFlight){
if (args[1] instanceof Function){
args[1] = wrapCallback(args[1]);
} else if (args[2] instanceof Function) {
args[2] = wrapCallback(args[2]);
}
activeCount++;
origRead.apply(fs,args);
} else {
//console.log("Delaying read:",args[0]);
pending.push(function(){
fs.readFile.apply(fs,args);
});
}
};
fs.writeFile = function(){
var args = Array.prototype.slice.call(arguments);
if (activeCount < global.maxFilesInFlight){
if (args[1] instanceof Function){
args[1] = wrapCallback(args[1]);
} else if (args[2] instanceof Function) {
args[2] = wrapCallback(args[2]);
}
activeCount++;
origWrite.apply(fs,args);
} else {
//console.log("Delaying write:",args[0]);
pending.push(function(){
fs.writeFile.apply(fs,args);
});
}
};
var hashes = [];
for(var i = 0; i < count; i++) {
(function(i) {
fs.readFile(filesPath + i + '.dat', 'utf8', function(err, data) {
if(err) {
console.error(err);
process.exit(1);
}
else {
hashes.push(hash(data, 512 /* block size */));
if(hashes.length == count) {
final();
}
if(hashes.length % 100 == 0) {
console.log('processed', hashes.length)
}
}
})
})(i);
}
function final() {
console.log('finalizing');
var string = ''
for(var i = 0; i < count; i++) {
string += hashes[i] + '\r\n';
}
fs.writeFileSync('./hashes.txt', string);
}
function stringToBytes(string) {
var bytes = [];
for(var i = 0; i < string.length; i++) {
bytes.push(string.charCodeAt(i));
}
return bytes;
}
function hash(prev, string, b) {
if(prev.length != b) throw new Error('The previous hash functions result length does not match the hashsize b ' + prev.length + ' to ' + b);
var output = [];
var stringValue;
for(var i = 0; i < prev.length; i++) {
stringValue = 0;
for(var n = i; n < string.length; n += b) {
stringValue += string[n];
}
output[i] = (prev[i] << 4)%256 ^ (stringValue % 256);
}
return output;
}
function finalize(blocks, length) {
var compressedBlocks = [];
for(var i = 0; i < length; i++) {
compressedBlocks[i] = 0;
}
for(var i = 0; i < length; i++) {
compressedBlocks[i%length] = compressedBlocks[i%length] ^ blocks[i%blocks.length][i%length];
}
var output = ''
for(var i = 0; i < compressedBlocks.length; i++) {
output += compressedBlocks[i]%10;
}
return output;
}
module.exports = function blockHash(string, a, size) {
if(arguments.length == 1) {
a = 32; // default block size = 32 bytes
size = 25;
}
if(arguments.length == 2) {
size = 25;
}
var bytes = stringToBytes(string);
// padding
var neededPadding = a - (bytes.length%a);
for(var i = 0; i < neededPadding; i++) {
bytes.push(0);
}
var outputBlocks = [];
var iv = [];
// build IV
for(var i = 0; i < size; i++) {
iv.push(0);
}
// process blocks
for(var blockNum = 0; blockNum < bytes.length/a; blockNum++) {
outputBlocks[blockNum] = hash((blockNum == 0 ? iv : outputBlocks[blockNum-1]), bytes.slice(blockNum*a, (blockNum + 1) * a), size);
}
return finalize(outputBlocks, size);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment