• 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
    

    The output:

    >echo(0);
    keys
    86,241,83,165,231,187,118,151,10,24,224,247,5,155,169,173
    98,173,140,6,192,202,181,73,5,180,107,39,255,128,101,152
    Enter go(); into left screen, press enter
    =undefined
    >go();
    Randomkeys
    42,27,58,194,230,236,105,20,255,239,187,194,230,236,105,131
    35,107,93,130,7,198,150,121,118,23,83,143,243,104,133,129
    Hash keys
    3,173,75,244,230,236,105,20,199,51,208,244,230,236,105,108
    78,11,5,145,114,186,64,56,35,74,240,253,216,142,195,239
    Myhashes
    0 plaintext
    94,233,45,61,231,236,105,20,129,249,178,61,231,236,105,254
    221,170,249,50,122,205,13,201,183,162,161,143,108,90,139,132
    0 hash(PT1), hash(PT1&PT2)
    71,76,23,135,37,184,17,104,9,201,154,228,75,139,43,70
    210,79,102,138,26,8,203,47,131,217,136,28,223,42,118,1
    1 plaintext
    77,121,156,159,231,236,105,20,127,138,34,160,231,236,105,29
    60,154,90,164,13,40,94,68,30,61,8,23,20,20,250,142
    1 hash(PT1), hash(PT1&PT2)
    187,159,41,38,232,148,215,100,160,137,110,238,132,225,187,30
    131,47,12,173,43,34,244,214,196,202,243,66,152,44,80,24
    2 plaintext
    118,53,38,1,232,236,105,20,117,90,163,1,232,236,105,210
    100,3,81,31,17,127,119,23,221,106,166,29,214,11,65,159
    2 hash(PT1), hash(PT1&PT2)
    229,120,199,141,209,44,28,26,243,118,209,239,21,203,233,121
    106,110,17,4,66,253,193,144,149,44,178,108,5,152,222,13
    3 plaintext
    100,248,143,98,232,236,105,20,100,178,16,99,232,236,105,58
    234,172,140,20,0,12,253,183,231,119,12,218,29,75,127,80
    3 hash(PT1), hash(PT1&PT2)
    48,226,109,106,2,245,197,206,251,241,239,74,211,245,123,138
    155,135,118,138,242,147,191,120,240,157,133,162,141,219,83,151
    
    

    1 Attachment

About