/*******************************************************************************
* keyBean class implements the keyBean message digest of RSA Data Security, Inc in RFC 132 1 submitted to IETF.
* algorithm.
******************************************************************************/
Public class keyBean {
/*
* The following S 1 1-S44 is actually a 4*4 matrix, which was realized by #define in the original C implementation, and it is static here.
* final means read-only and must be shared between multiple instances in the same process space.
*/
Static final int s11= 7;
Static final int s12 =12;
Static final int s13 =17;
Static final int s14 = 22;
Static final int s 21= 5;
Static final int S22 = 9;;
Static final ints23 =14;
Static final int S24 = 20
Static final int s 31= 4;
Static final ints32 =11;
Static final ints33 =16;
Static final int S34 = 23
Static final int s 41= 6;
Static final ints42 =10;
Static final ints43 =15;
Static final ints44 = 21;
Static last byte [] padding = {-128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0 };
/*
* The following three members are the three core data used in calculating keyBean, which are defined in the keyBean_CTX structure in the original C implementation.
*/
private long[]state = new long[4]; //status (ABCD)
private long[]count = new long[2]; //digits, modulo 2^64 (lsb
//First)
Private byte [] buffer = new byte [64]; //input buffer
/*
* digestHexStr is the only public member of keyBean, and it is the 16 ASCII representation of the latest calculation result.
*/
Common string digestHexStr
/*
* digest is the binary internal representation of the latest calculation result, indicating the value of 128bit keyBean.
*/
Private byte [] digest = new byte [16];
/*
* getkeyBeanofStr is the main public * * * method of the keyBean class, and the entry parameter is the string you want to convert the keyBean.
* Returns the transformed result, which is obtained from the member of digesthextr(public * * *).
*/
Common string getkeybeanofstr (stringinbuf) (
keybean init();
keyBeanUpdate(inbuf.getBytes()、in buf . length());
keybean final();
digestHexStr =
for(int I = 0; I< 16; i++) {
digesthextr+= byte hex(digest[I]);
}
Return to digestHexStr
}
//This is the standard constructor of the keyBean class. JavaBean needs a public constructor with no parameters.
Public key Bean() {
keybean init();
Return;
}
/* keyBeanInit is an initialization function that initializes core variables and loads standard magic numbers */
Private void keyBeanInit() {
count[0]= 0L;
count[ 1]= 0L;
///* Load the magic initialization constant.
state[0]= 0x 6745230 1L;
state[ 1]= 0x efcdab 89 l;
state[2]= 0x 98 badcfel;
state[3]= 0x 10325476 l;
Return;
}
/*
* F, g, h, I are four basic keyBean functions. In the original C implementation of keyBean, because they are
* In order to improve efficiency, simple bit operations can be implemented as macros. In java, we implement them as private methods and keep their names in the original C.
*/
private long F(long x,long y,long z) {
Return (x & ampy) | ((~ x)&; z);
}
private long G(long x,long y,long z) {
Returns (x & ampz) | (y&; (~ z));
}
private long H(long x,long y,long z) {
Returns x y z;
}
Private I (length x, length y, length z) {
Returns y (x | (~ z));
}
/*
* FF, gg, hh and ii will call F, G, H and I for further conversion. FF, GG, HH and II pairs.
* Round 1, 2, 3 and 4. Rotation and addition are separated to prevent
* Recalculate.
*/
Private length FF (long A, long B, long C, long D, long X, long S, long ac) {
a += F(b,c,d)+x+AC;
a =((int)a & lt; & lts)|((int)a & gt; & gt& gt(32-s));
a+= b;
Return to a;
}
Private length GG (long A, long B, long C, long D, long X, long S, long ac) {
a += G(b,c,d)+x+AC;
a =((int)a & lt; & lts)|((int)a & gt; & gt& gt(32-s));
a+= b;
Return to a;
}
Private long HH (long A, long B, long C, long D, long X, long S, long ac) {
a += H(b,c,d)+x+AC;
a =((int)a & lt; & lts)|((int)a & gt; & gt& gt(32-s));
a+= b;
Return to a;
}
Private long two (long A, long B, long C, long D, long X, long S, long ac) {
a += I(b,c,d)+x+AC;
a =((int)a & lt; & lts)|((int)a & gt; & gt& gt(32-s));
a+= b;
Return to a;
}
/*
* keyBeanUpdate is the main calculation process of keyBean, inbuf is the byte string to be converted, inputlen is the length, and this.
* function is called by getkeyBeanofStr, and keyBeaninit needs to be called before calling, so it is designed as private.
*/
private void keybean update(byte[]in buf,int inputLen) {
int i,index,partLen
Byte[] block = new byte [64];
index =(int)(count[0]& gt; & gt& gt3)& amp; 0x3F
///* Update digits */
if((count[0]+=(input len & lt; & lt3))& lt; (inputLen & lt& lt3))
count[ 1]++;
count[ 1]+=(input len & gt; & gt& gt29);
part len = 64-index;
//Transform as many times as possible.
if(input len & gt; = partLen) {
keyBeanMemcpy(buffer,inbuf,index,0,partLen);
KeyBeanTransform (buffer);
for(I = part len; I+63 & lt; inputLeni += 64) {
keyBeanMemcpy(block,inbuf,0,I,64);
keybean transform(block);
}
Index = 0;
} Otherwise,
I = 0;
///* Buffer the remaining input */
keyBeanMemcpy(buffer,inbuf,index,I,input len-I);
}
/*
* keyBeanFinal collates and fills in the output results.
*/
Private void keyBeanFinal() {
Byte[] bits = new byte [8];
int index,padLen
///* Number of saved digits */
Code (bit, count, 8);
///* output to 56 mod 64.
index =(int)(count[0]& gt; & gt& gt3)& amp; 0x3f
padLen =(index & lt; 56) ? (56-index): (120-index);
keyBeanUpdate(PADDING,padLen);
///* Additional length (before filling) */
KeyBeanUpdate (bit, 8);
///* Store status in summary */
Code (abstract, status,16);
}
/*
* keyBeanMemcpy is a block copy function of byte array used internally, starting with inpos of input and putting len length.
* Bytes are copied to the outpos location of the output.
*/
private void keybean memcpy(byte[]output,byte[] input,int outpos,
int inpos,int len) {
int I;
for(I = 0; I & ltleni++)
Output [output+I] = input [input+I];
}
/*
* keyBeanTransform is the keyBean core converter, named keyBeanUpdate, and block is the original byte of the block.
*/
Private void keyBeanTransform (byte block []) {
Long a = state [0], b = state [1], c = state [2], d = state [3];
long[]x = new long[ 16];
Decoding (x, block, 64);
/* Round 1 */
a = FF(a,b,c,d,x[0],S 1 1,0 xd 76 a 478 l); /* 1 */
d = FF(d,a,b,c,x[ 1],S 12,0 xe8 c 7 b 756 l); /* 2 */
c = FF(c,d,a,b,x[2],S 13,0x 242070 dbl); /* 3 */
b = FF(b,c,d,a,x[3],S 14,0xc 1 BDC eeel); /* 4 */
a = FF(a,b,c,d,x[4],S 1 1,0 xf 57 c 0 fafl); /* 5 */
d = FF(d,a,b,c,x[5],S 12,0x 4787 c 62 al); /* 6 */
c = FF(c,d,a,b,x[6],S 13,0xa 83046 13L); /* 7 */
b = FF(b,c,d,a,x[7],S 14,0x FD 46950 1L); /* 8 */
a = FF(a,b,c,d,x[8],S 1 1,0x 698098 d8l); /* 9 */
d = FF(d,a,b,c,x[9],S 12,0x8b 44 f 7 AFL); /* 10 */
c = FF(c,d,a,b,x[ 10],S 13,0x ffff 5b b 1L); /* 1 1 */
b = FF(b,c,d,a,x[ 1 1],S 14,0x 895 CD 7 bel); /* 12 */
a = FF(a,b,c,d,x[ 12],S 1 1,0x6b 90 1 122 l); /* 13 */
d = FF(d,a,b,c,x[ 13],S 12,0x FD 987 193 l); /* 14 */
c = FF(c,d,a,b,x[ 14],S 13,0xa 679438 El); /* 15 */
b = FF(b,c,d,a,x[ 15],S 14,0x49b 4082 1L); /* 16 */
/* The second round */
a = GG(a,b,c,d,x[ 1],S2 1,0xf 6 1e 2562 l); /* 17 */
d = GG(d,a,b,c,x[6],S22,0xc 040 b 340 l); /* 18 */
c = GG(c,d,a,b,x[ 1 1],S23,0x 265 e5a 5 1L); /* 19 */
b = GG(b,c,d,a,x[0],S24,0x e 9 b 6 c 7 AAL); /* 20 */
a = GG(a,b,c,d,x[5],S2 1,0x d 62 f 105 dl); /* 2 1 */
d = GG(d,a,b,c,x[ 10],S22,0x 244 1453 l); /* 22 */
c = GG(c,d,a,b,x[ 15],S23,0xd8a 1e 68 1L); /* 23 */
b = GG(b,c,d,a,x[4],S24,0x e 7d 3 FBC 8 l); /* 24 */
a = GG(a,b,c,d,x[9],S2 1,0x 2 1e 1 cde6l); /* 25 */
d = GG(d,a,b,c,x[ 14],S22,0xc 33707 d6l); /* 26 */
c = GG(c,d,a,b,x[3],S23,0x f 4d 50d 87 l); /* 27 */
b = GG(b,c,d,a,x[8],S24,0x 455 a 14 EDL); /* 28 */
a = GG(a,b,c,d,x[ 13],S2 1,0x a 9 e 3 e 905 l); /* 29 */
d = GG(d,a,b,c,x[2],S22,0x FCE fa 3f 8 l); /* 30 */
c = GG(c,d,a,b,x[7],S23,0x 676 f 02d 9l); /* 3 1 */
b = GG(b,c,d,a,x[ 12],S24,0x 8d 2 a4 c8 al); /* 32 */
/* The third round */
a = HH(a,b,c,d,x[5],S3 1,0x fffa 3942 l); /* 33 */
d = HH(d,a,b,c,x[8],S32,0x 877 1f 68 1L); /* 34 */
c = HH(c,d,a,b,x[ 1 1],S33,0x6d 9d 6 122 l); /* 35 */
b = HH(b,c,d,a,x[ 14],S34,0x FDE 5380 cl); /* 36 */
a = HH(a,b,c,d,x[ 1],S3 1,0x a4 beea 44 l); /* 37 */
d = HH(d,a,b,c,x[4],S32,0x 4 bdecfa 9 l); /* 38 */
c = HH(c,d,a,b,x[7],S33,0 xf 6 bb 4 b 60 l); /* 39 */
b = HH(b,c,d,a,x[ 10],S34,0x befbc 70 l); /* 40 */
a = HH(a,b,c,d,x[ 13],S3 1,0x 289 b 7 EC 6 l); /* 4 1 */
d = HH(d,a,b,c,x[0],S32,0x EAA 127 fal); /* 42 */
c = HH(c,d,a,b,x[3],S33,0x d4ef 3085 l); /* 43 */
b = HH(b,c,d,a,x[6],S34,0x 488 1d 05l); /* 44 */
a = HH(a,b,c,d,x[9],S3 1,0x d 9d 4d 039 l); /* 45 */
d = HH(d,a,b,c,x[ 12],S32,0x e 6 db 99 e 5 l); /* 46 */
c = HH(c,d,a,b,x[ 15],S33,0x 1fa 27 cf 8 l); /* 47 */
b = HH(b,c,d,a,x[2],S34,0x C4 AC 5665 l); /* 48 */
/* Round 4 */
a = II(a,b,c,d,x[0],S4 1,0xf 4292244 l); /* 49 */
d = II(d,a,b,c,x[7],S42,0x 432 aff 97 l); /* 50 */
c = II(c,d,a,b,x[ 14],S43,0x ab 9423 a7l); /* 5 1 */
b = II(b,c,d,a,x[5],S44,0x fc 93 a 039 l); /* 52 */
a = II(a,b,c,d,x[ 12],S4 1,0x 655 b 59 c3l); /* 53 */
d = II(d,a,b,c,x[3],S42,0x 8 f 0 CCC 92 l); /* 54 */
c = II(c,d,a,b,x[ 10],S43,0x ffeff 47 dl); /* 55 */
b = II(b,c,d,a,x[ 1],S44,0x 85845 DD 1L); /* 56 */
a = II(a,b,c,d,x[8],S4 1,0x 6 fa 87 E4 fl); /* 57 */
d = II(d,a,b,c,x[ 15],S42,0 xfe 2 ce 6 E0 l); /* 58 */
c = II(c,d,a,b,x[6],S43,0xa 30 143 14L); /* 59 */
b = II(b,c,d,a,x[ 13],S44,0x4e 08 1 1a 1L); /* 60 */
a = II(a,b,c,d,x[4],S4 1,0x f 7537 e82l); /* 6 1 */
d = II(d,a,b,c,x[ 1 1],S42,0 xbd 3 af 235 l); /* 62 */
c = II(c,d,a,b,x[2],S43,0x 2 ad 7 D2 bbl); /* 63 */
b = II(b,c,d,a,x[9],S44,0 xeb 86d 39 1L); /* 64 */
state[0]+= a;
state[ 1]+= b;
State [2]+= c;
State [3]+= d;
}
/*
* Encode decomposes a long array into byte arrays in sequence, because the long type of java is 64bit, only 32bit lower, to adapt to the purpose of the original C.
*/
Private null code (byte[] output, long[] input, int len) {
int i,j;
for (i = 0,j = 0; j & ltleni++,j += 4) {
Output [j] = (bytes) (input [i]&; 0x ffl);
output[j+ 1]=(byte)((input[I]& gt; & gt& gt8)& amp; 0x ffl);
output[j+2]=(byte)((input[I]& gt; & gt& gt 16); 0x ffl);
output[j+3]=(byte)((input[I]& gt; & gt& gt24)& amp; 0x ffl);
}
}
/*
* Decode combines the number of bytes into an array in sequence, because the long type of java is 64bit.
* Only the low 32 bits and the high 32 bits are reset to meet the purpose of the original C implementation.
*/
Private void Decoding (long[] Output, byte[] Input, int len) {
int i,j;
for (i = 0,j = 0; j & ltleni++,j += 4)
output[I]= b2iu(input[j])|(b2iu(input[j+ 1])& lt; & lt8)
|(b2iu(input[j+2])& lt; & lt 16)|(b2iu(input[j+3])& lt; & lt24);
Return;
}
/*
* b2iu is a program that I wrote to "upgrade" bytes according to the principle of regardless of symbols, because there is no unsigned operation in java.
*/
Public static length b2iu (byte b) (
Back to b<0? b & amp0x7F+ 128:b;
}
/*
* byteHEX (), which is used to convert a byte number into hexadecimal ASCII representation.
* Because toString of byte in java can't achieve this, and we don't have sprintf(out BF, "%02X", ib) in C language.
*/
Public static string byteHEX (byte ib) (
char[] Digit = { '0 ',' 1 ',' 2 ',' 3 ',' 4 ',' 5 ',' 6 ',' 7 ',' 8 ',' 9 ',' A ',
b ',' C ',' D ',' E ',' F ' };
char[]ob = new char[2];
ob[0]= Digit[(IB & gt; & gt& gt4)& amp; 0X0F];
ob[ 1]= Digit[IB & amp; 0X0F];
String s = new string (ob);
Return to s;
}
Public static void main (strinargs []) {
key bean m = new key bean();
If (array.getlength (args) = = 0) {//If there are no parameters, execute the standard test suite.
System.out.println("keyBean test suite: ");
system . out . println(" key bean(\ ":+m . getkeybeanofstr(" ");
system . out . println(" key bean(\ " a \ "):"+m . getkeybeanofstr(" a "));
system . out . println(" key bean(\ " ABC \ "):"+m . getkeybeanofstr(" ABC "));
system . out . println(" key bean(\ " message digest \ "):"
+m.getkeyBeanofStr ("message digest");
system . out . println(" key bean(\ " abcdefghijklmnopqrstuvwxyz \ "):"
+m . getkeybeanofstr(" abcdefghijklmnopqrstuvwxyz "));
System.out
. println(" key bean(\ " abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz 0 123456789 \ "):"
+ m
. getkeyBeanofStr(" abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz 0 123456789 "));
} Otherwise,
system . out . println(" keyBean("+args[0]+")= "
+m . getkeybeanofstr(args[0]);
}
}