⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 sha256.java

📁 ElGamal method is an aymetric method to crypt message. You can use ElGamal to crypt the key session
💻 JAVA
字号:
package elgamal;public class sha256 {            private int[]sha=new int[16];    public static final int[] K = {         0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,         0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,         0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,         0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,         0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,         0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,         0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,         0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,         0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,         0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,         0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,         0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,         0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,         0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,         0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,         0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2    };        public sha256(byte[]test_byte){        int test_length=test_byte.length;        int l=8*test_length;        int k=(448-l-1)%512;        if(k<0)k+=512;        int k8=(k-7)>>>3;        int tot4=(test_length+1+k8+8)>>>2;        int[]in=new int[16];        sha[0] = 0x6a09e667;        sha[1]  = 0xbb67ae85;        sha[2] = 0x3c6ef372;        sha[3] = 0xa54ff53a;        sha[4] = 0x510e527f;        sha[5] = 0x9b05688c;        sha[6] = 0x1f83d9ab;        sha[7] = 0x5be0cd19;        int test_reste=test_length;        int offset=0;        int un_ok=0;        for(int i=0;i<tot4;i+=16){           int in_i=0;           if(i+16<tot4){            for(int j=0;j<16;j++){               switch (test_reste) {                   case 0 :                       if(un_ok==1)in[in_i++]=0;                       else in[in_i++]=0x80 << 24;                       un_ok=1;                       break;                   case 1 :                       in[in_i++]=(test_byte[offset++] << 24) | (0x80 << 16);                       test_reste=0;                       un_ok=1;                       break;                   case 2 :                       in[in_i++]=(test_byte[offset++] << 24) | ((test_byte[offset++] & 0xFF) << 16) | (0x80 << 8) ;                       test_reste=0;                       un_ok=1;                       break;                   case 3 :                       in[in_i++]=(test_byte[offset++] << 24) | ((test_byte[offset++] & 0xFF) << 16)                              | ((test_byte[offset++] & 0xFF) <<  8) | 0x80 ;                       test_reste=0;                       un_ok=1;                       break;                   default :                       in[in_i++]=(test_byte[offset++] << 24) | ((test_byte[offset++] & 0xFF) << 16)                              | ((test_byte[offset++] & 0xFF) <<  8) | (test_byte[offset++] & 0xFF) ;                       test_reste-=4;                       un_ok=0;                       break;               }             }           }else{             for(int j=0;j<14;j++){               switch (test_reste) {                   case 0 :                       if(un_ok==1)in[in_i++]=0;                       else in[in_i++]=0x80<<24;                       un_ok=1;                       break;                   case 1 :                       in[in_i++]=(test_byte[offset++] << 24) | (0x80 << 16);                       test_reste=0;                       un_ok=1;                       break;                   case 2 :                       in[in_i++]=(test_byte[offset++] << 24) | ((test_byte[offset++] & 0xFF) << 16) | (0x80 << 8) ;                       test_reste=0;                       un_ok=1;                       break;                   case 3 :                       in[in_i++]=(test_byte[offset++] << 24) | ((test_byte[offset++] & 0xFF) << 16)                              | ((test_byte[offset++] & 0xFF) <<  8) | 0x80 ;                       test_reste=0;                       un_ok=1;                       break;                   default :                       in[in_i++]=(test_byte[offset++] << 24) | ((test_byte[offset++] & 0xFF) << 16)                              | ((test_byte[offset++] & 0xFF) <<  8) | (test_byte[offset++] & 0xFF) ;                       test_reste-=4;                       un_ok=0;                       break;               }             }            in[in_i++]=0;            in[in_i++]=l;           }           sha=sha(sha,in);        }    }        public int[]getHash(){        return sha;    }        public static int[] sha(int []sha,int[] in) {      int a=sha[0];      int b=sha[1];      int c=sha[2];      int d=sha[3];      int e=sha[4];      int f=sha[5];      int g=sha[6];      int h=sha[7];      int T1, T2;      int []W=sha256_sch(in);      for (int j = 0; j < 64; j++) {         T1 = h + SIGMA1(e) + Ch(e,f,g) + K[j] + W[j];         T2 = SIGMA0(a) + Maj(a,b,c);         h = g;         g = f;         f = e;         e = (d + T1);         d = c;         c = b;         b = a;         a = (T1 + T2);               }      return new int[] { sha[0] + a, sha[1] + b, sha[2] + c, sha[3] + d, sha[4] + e, sha[5] + f, sha[6] + g, sha[7] + h };    }    public static int[]sha256_sch(int[]M){        int[]W=new int[64];        for(int i=0;i<16;i++)W[i]=M[i];        for(int i=16;i<64;i++)W[i]=(sigma1(W[i-2])+W[i-7]+sigma0(W[i-15])+W[i-16]);        return W;    }    public static int Ch(int x,int y,int z){        return ((x & y)^((~x) & z));      }    public static int Maj(int x,int y,int z){        return ((x & y)^(x & z)^(y & z));    }    public static int SIGMA0(int x){        return (S(2,x)^S(13,x)^S(22,x));    }    public static int SIGMA1(int x){        return (S(6,x)^S(11,x)^S(25,x));    }    public static int sigma0(int x){        return S(7,x)^S(18,x)^R(3,x);    }    public static int sigma1(int x){        return (S(17,x)^S(19,x)^R(10,x));    }    public static int S(int n,int x){        int xx=(x >>> n) | (x << (32-n));        return xx;    }    public static int R(int n,int x){        return (x >>> n);    }  }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -