myhash1.js
Uses permutations, oneway, and random functions.
var MH=new Myhash(1,1,0,0,0). Try varying the numbers. First 4 can be 0 or 1.
5th number can be 0,1,2 and changes the oneway function operator.
// aes1 0= encrypt, 1=decrypt
// aes2 0= encrypt, 1=decrypt
// pdir1 permutation direction 0 or 1
// pdir2 permutation direction 0 or 1
// owtype 0= add, 1= xor, 2=multiply
//myhash1.js
//a permutation class
function Perm(n){
this.N=n;
this.C=new Uint8Array(n);
}
Perm.prototype.swap=function(i,j){
var a=0;
a=this.C[i];
this.C[i]=this.C[j];
this.C[j]=a;
};//end swap
//Initialize a random permutation
Perm.prototype.createPerm=function(){
var i,j;
var k=this.N;
E.srand(E.hwRand());
for(j=0;j<this.N;j++)this.C[j]=j;
for(i=0;i<(this.N-1);i++){
j=parseInt((Math.random()*256)%k,10);
this.swap(i,i+j);
k--;
}//next i
};//end createPerm
//Use permutation to relocate bits in E to D
//dir determines the indirection direction
Perm.prototype.doPerm=function(dir,D,E){
var i,j,k,m;
for(i=0;i<this.N/8;i++)D[i]=0;
for(i=0;i<this.N;i++){
j=this.C[i];
k=i%8;
m=j%8;
var a=1;
var b=1;
a=1<<k;
b=1<<m;
if(dir){
if(E[(i-k)/8]&a) D[(j-m)/8]=D[(j-m)/8]|b;
}else{
if(E[(j-m)/8]&b) D[(i-k)/8]=D[(i-k)/8]|a;
}//end else
}//next i
};//end doPerm
//Use permutation of length N in E to relocate bytes in C to D
Perm.prototype.doPermBytes=function(dir,E,C){
var i,j,k,m;
for(i=0;i<this.N;i++)D[i]=0;
for(i=0;i<this.N;i++){
j=this.C[i];
if(dir){
D[i]=E[j];
}else{
D[j]=E[i];
}//end else
}//next i
};//end doPermBytes
///////////////////////////
//a oneway class
function Oneway(){
this.A=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];
}
Oneway.prototype.oneway1=function(mode,A,B,C,D,E){
switch(mode){
case 0:
return A+B+C+D+E;
case 1:
return A^B^C^D^E;
case 2:
return A*B*C*D*E;
}//end switch
};//end oneway1
Oneway.prototype.modit=function(A){
for(var i=0;i<16;i++)A[i]=this.A[i]%256;
};//end modit
Oneway.prototype.fillit=function(b){
for(var i=0;i<16;i++)this.A[i]=b;
};//end fillit
Oneway.prototype.oneway=function(mode,B){
var oneway1=this.oneway1;
//rows
this.A[0]=oneway1(mode,this.A[0],B[0],B[1],B[2],B[3]);
this.A[1]=oneway1(mode,this.A[1],B[4],B[5],B[6],B[7]);
this.A[2]=oneway1(mode,this.A[2],B[8],B[9],B[10],B[11]);
this.A[3]=oneway1(mode,this.A[3],B[12],B[13],B[14],B[15]);
//columns
this.A[4]=oneway1(mode,this.A[4],B[0],B[4],B[8],B[12]);
this.A[5]=oneway1(mode,this.A[5],B[1],B[5],B[9],B[13]);
this.A[6]=oneway1(mode,this.A[6],B[2],B[6],B[10],B[14]);
this.A[7]=oneway1(mode,this.A[7],B[3],B[7],B[11],B[15]);
//diagonals\
this.A[8]=oneway1(mode,this.A[8],B[0],B[5],B[10],B[15]);
this.A[9]=oneway1(mode,this.A[9],B[1],B[6],B[11],B[12]);
this.A[10]=oneway1(mode,this.A[10],B[2],B[7],B[8],B[13]);
this.A[11]=oneway1(mode,this.A[11],B[3],B[4],B[9],B[14]);
//diagonals/
this.A[12]=oneway1(mode,this.A[12],B[3],B[6],B[9],B[12]);
this.A[13]=oneway1(mode,this.A[13],B[0],B[7],B[10],B[13]);
this.A[14]=oneway1(mode,this.A[14],B[1],B[4],B[11],B[14]);
this.A[15]=oneway1(mode,this.A[15],B[2],B[5],B[8],B[15]);
};//end oneway
// Random object
// aes1 0= encrypt, 1=decrypt
// aes2 0= encrypt, 1=decrypt
// pdir1 permutation direction 0 or 1
// pdir2 permutation direction 0 or 1
// owtype 0= add, 1= xor, 2=multiply
function Random(aes1,aes2,pdir1,pdir2,owtype){
this.AES1=aes1;
this.AES2=aes2;
this.Pdir1=pdir1;
this.Pdir2=pdir2;
this.Owtype=owtype;
this.ow=new Oneway();
this.plain=new Uint8Array(16);
this.R=new ArrayBuffer(16);
this.perm1=new Perm(128);
this.key1=new Uint8Array(16);
this.perm2=new Perm(128);
this.key2=new Uint8Array(16);
}
Random.prototype.random=function(){
var tt=Date.now()*1000000;
for(var i=0;i<16;i++){
if(i===8)tt=Date.now()*1000000;
this.R[i]=tt%256;
tt=tt-this.R[i];
tt=tt/256;
}//nexti
E.srand(E.hwRand());
this.R[0]=(Math.random()*256)%256;
this.R[8]=(Math.random()*256)%256;
this.R[15]=(Math.random()*256)%256;
// printit(R);
this.perm1.doPerm(this.Pdir1,this.plain,this.R);
//printit(plain);
if(this.AES1){this.R=AES.encrypt(this.plain,this.key1);
}else{
this.R=AES.decrypt(this.plain,this.key1);
}
//printit(this.R);
this.ow.fillit(1);
this.ow.oneway(this.Owtype,this.R);
this.ow.modit(this.R);
//printit(this.R);
this.perm2.doPerm(this.Pdir2,this.plain,this.R);
//printit(plain);
if(this.AES2){this.R=AES.encrypt(this.plain,this.key2);
}else{
this.R=AES.decrypt(this.plain,this.key2);
}
//printit(this.R);
return this.R;
};//end random
Random.prototype.setup_random=function(){
for(var i=0;i<16;i++){
E.srand(E.hwRand());
this.key1[i]=parseInt((Math.random()*256),10);
E.srand(E.hwRand());
this.key2[i]=parseInt((Math.random()*256),10);
}//nexti
this.perm1.createPerm();
this.perm2.createPerm();
};
///////////////////////////////
//Myhash object
// aes1 0= encrypt, 1=decrypt
// aes2 0= encrypt, 1=decrypt
// pdir1 permutation direction 0 or 1
// pdir2 permutation direction 0 or 1
// owtype 0= add, 1= xor, 2=multiply
function Myhash(aes1,aes2,pdir1,pdir2,owtype){
this.AES1=aes1;
this.AES2=aes2;
this.Pdir1=pdir1;
this.Pdir2=pdir2;
this.Owtype=owtype;
this.ow=new Oneway();
this.plain=new Uint8Array(16);
this.plain1=new Uint8Array(16);
this.R=new ArrayBuffer(16);
this.perm1=new Perm(128);
this.key1=new Uint8Array(16);
this.perm2=new Perm(128);
this.key2=new Uint8Array(16);
}
Myhash.prototype.myhash=function(R,flag){
var i;
this.perm1.doPerm(this.Pdir1,this.plain,R);
if(this.AES1){this.R=AES.encrypt(this.plain,this.key1);
}else{
this.R=AES.decrypt(this.plain,this.key1);
}
if(flag){
this.ow.fillit(1);
for(i=0;i<16;i++)this.plain1[i]=0;
}
this.ow.oneway(this.Owtype,this.R);
this.ow.modit(this.R);
this.perm2.doPerm(this.Pdir2,this.plain,this.R);
if(this.AES2){this.R=AES.encrypt(this.plain,this.key2);
}else{
this.R=AES.decrypt(this.plain,this.key2);
}
for(i=0;i<16;i++)this.R[i]=this.R[i]^ this.plain1[i];
copy(this.plain1,this.R);
return this.R;
};//end random
Myhash.prototype.setup_myhash=function(R){
this.key1=R.random();
this.key2=R.random();
console.log("Hash keys");
printit(this.key1);
printit(this.key2);
this.perm1.createPerm();
this.perm2.createPerm();
};
///////////////////////////////
function printit(W){
console.log(W[0]+','+W[1]+','+W[2]+','+W[3]+','+
W[4]+','+W[5]+','+W[6]+','+W[7]+','+
W[8]+','+W[9]+','+W[10]+','+W[11]+','+
W[12]+','+W[13]+','+W[14]+','+W[15]
);
}//end printit
function copy(A,B){for(var i=0;i<16;i++)A[i]=B[i];}
function compare(A,B){
for(var i=0;i<16;i++)if(A[i]!=B[i])return 0;
return 1;
}
///////////////////////////////
// Random object
// aes1 0= encrypt, 1=decrypt
// aes2 0= encrypt, 1=decrypt
// pdir1 permutation direction 0 or 1
// pdir2 permutation direction 0 or 1
// owtype 0= add, 1= xor, 2=multiply
//function Random(aes1,aes2,pdir1,pdir2,owtype){
var R=new Random(1,1,0,0,0);
R.setup_random();
console.log("keys");
printit(R.key1);
printit(R.key2);
console.log("Enter go(); into left screen, press enter");
function go(){
R.key1=R.random();
R.key2=R.random();
R.key1=R.random();
R.key2=R.random();
console.log("Randomkeys");
printit(R.key1);
printit(R.key2);
///////////////////////////////
// Myhash object
// aes1 0= encrypt, 1=decrypt
// aes2 0= encrypt, 1=decrypt
// pdir1 permutation direction 0 or 1
// pdir2 permutation direction 0 or 1
// owtype 0= add, 1= xor, 2=multiply
var MH=new Myhash(1,1,0,0,0);
MH.setup_myhash(R);
console.log("Myhashes");
C=new Uint8Array(16);
D=new Uint8Array(16);
C1=new Uint8Array(16);
D1=new Uint8Array(16);
for(var j=0;j<4;j++){
C=R.random(); //fisrt block to hash
C1=R.random();//second block to hash
D=MH.myhash(C,1);//initializes hash
D1=MH.myhash(C1,0);//adds to hash
console.log(j+" plaintext");
printit(C);
printit(C1);
console.log(j+" hash(PT1), hash(PT1&PT2)");
printit(D);
printit(D1);
}
}//end go
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working on.
myhash1.js
Uses permutations, oneway, and random functions.
var MH=new Myhash(1,1,0,0,0). Try varying the numbers. First 4 can be 0 or 1.
5th number can be 0,1,2 and changes the oneway function operator.
// aes1 0= encrypt, 1=decrypt
// aes2 0= encrypt, 1=decrypt
// pdir1 permutation direction 0 or 1
// pdir2 permutation direction 0 or 1
// owtype 0= add, 1= xor, 2=multiply
The output:
1 Attachment