md5 Sha

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

import java.util.

*;
public class MD5
{
private static final int INIT_A

= 0x67452301;

private static final int INIT_B

= (int) 0xEFCDAB89L;

private static final int INIT_C

= (int) 0x98BADCFEL;

private static final int INIT_D

= 0x10325476;

private static final int[] SHIFT_AMTS = { 7, 12, 17, 22, 5, 9, 14, 20, 4, 11, 16, 23,
6, 10, 15, 21 };
private static final int[] TABLE_T

= new int[64];

static
{
for (int i = 0; i < 64; i++)
TABLE_T[i] = (int) (long) ((1L << 32) * Math.abs(Math.sin(i + 1)));
}
public static byte[] computeMD5(byte[] message)
{
int messageLenBytes = message.length;
int numBlocks = ((messageLenBytes + 8) >>> 6) + 1;
int totalLen = numBlocks << 6;
byte[] paddingBytes = new byte[totalLen - messageLenBytes];
paddingBytes[0] = (byte) 0x80;
long messageLenBits = (long) messageLenBytes << 3;
for (int i = 0; i < 8; i++)
{
paddingBytes[paddingBytes.length - 8 + i] = (byte) messageLenBits;
messageLenBits >>>= 8;
}
int a = INIT_A;
int b = INIT_B;
int c = INIT_C;
int d = INIT_D;
int[] buffer = new int[16];
for (int i = 0; i < numBlocks; i++)
{

int index = i << 6;


for (int j = 0; j < 64; j++, index++)
buffer[j >>> 2] = ((int) ((index < messageLenBytes) ? message[index] :
paddingBytes[index messageLenBytes]) << 24) | (buffer[j >>> 2] >>> 8);
int originalA = a;
int originalB = b;
int originalC = c;
int originalD = d;
for (int j = 0; j < 64; j++)
{
int div16 = j >>> 4;
int f = 0;
int bufferIndex = j;
switch (div16)
{
case 0:
f = (b & c) | (~b & d);
break;
case 1:
f = (b & d) | (c & ~d);
bufferIndex = (bufferIndex * 5 + 1) & 0x0F;
break;
case 2:
f = b ^ c ^ d;
bufferIndex = (bufferIndex * 3 + 5) & 0x0F;
break;
case 3:
f = c ^ (b | ~d);
bufferIndex = (bufferIndex * 7) & 0x0F;
break;
}
int temp = b + Integer.rotateLeft(a + f + buffer[bufferIndex] + TABLE_T[j],
SHIFT_AMTS[(div16 << 2) | (j & 3)]);
a = d;
d = c;
c = b;
b = temp;
}
a += originalA;
b += originalB;
c += originalC;
d += originalD;
}
byte[] md5 = new byte[16];

int count = 0;
for (int i = 0; i < 4; i++)
{
int n = (i == 0) ? a : ((i == 1) ? b : ((i == 2) ? c : d));
for (int j = 0; j < 4; j++)
{
md5[count++] = (byte) n;
n >>>= 8;
}
}
return md5;
}

public static String toHexString(byte[] b)


{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < b.length; i++)
{
sb.append(String.format("%02X", b[i] & 0xFF));
}
return sb.toString();
}

public static void main(String[] args)


{

System.out.print("enter value=");
Scanner sc= new Scanner(System.in);
String testStrings = sc.next();
String s = testStrings;
System.out.println(""+ s + " ==> \""+"0x" +
toHexString(computeMD5(s.getBytes()))+"\"");
return;
}
}

import
import
import
import

java.util.*;
java.io.*;
java.nio.ByteBuffer;
java.nio.MappedByteBuffer;

public class SHA {

int j, temp;
int A, B, C, D, E;
int[] H = {0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0};
int F;
public SHA() {
//super("SHA-1");
Scanner sc=new Scanner(System.in);
System.out.print("enter value=");
String e=sc.next();
actionPerformed(e);
}
public class Digest {
String digestIt(byte[] dataIn) {
byte[] paddedData = padTheMessage(dataIn);
int[] H = {0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476,
0xC3D2E1F0};
int[] K = {0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xCA62C1D6};
if (paddedData.length % 64 != 0) {
System.out.println("Invalid padded data length.");
System.exit(0);
}
int passesReq = paddedData.length / 64;
byte[] work = new byte[64];
for (int passCntr = 0; passCntr < passesReq; passCntr++) {
System.arraycopy(paddedData, 64 * passCntr, work, 0, 64);
processTheBlock(work, H, K);
}
return intArrayToHexStr(H);
}
//-------------------------------------------//
private byte[] padTheMessage(byte[] data) {
int origLength = data.length;
int tailLength = origLength % 64;
int padLength = 0;
if ((64 - tailLength >= 9)) {
padLength = 64 - tailLength;
} else {
padLength = 128 - tailLength;
}

byte[] thePad = new byte[padLength];


thePad[0] = (byte) 0x80;
long lengthInBits = origLength * 8;
for (int cnt = 0; cnt < 8; cnt++) {
thePad[thePad.length - 1 - cnt] = (byte) ((lengthInBits >> (8 * cnt)) &
0x00000000000000FF);
}
byte[] output = new byte[origLength + padLength];
System.arraycopy(data, 0, output, 0, origLength);
System.arraycopy(thePad, 0, output, origLength, thePad.length);
return output;
}
//-------------------------------------------//
private void processTheBlock(byte[] work, int H[], int K[]) {
int[] W = new int[80];
for (int outer = 0; outer < 16; outer++) {
int temp = 0;
for (int inner = 0; inner < 4; inner++) {
temp = (work[outer * 4 + inner] & 0x000000FF) << (24 - inner * 8);
W[outer] = W[outer] | temp;
}
}
for (int j = 16; j < 80; j++) {
W[j] = rotateLeft(W[j - 3] ^ W[j - 8] ^ W[j - 14] ^ W[j - 16], 1);
}
A = H[0];
B = H[1];
C = H[2];
D = H[3];
E = H[4];
for (int j = 0; j < 20; j++) {
F = (B & C) | ((~B) & D);
// K = 0x5A827999;
temp = rotateLeft(A, 5) + F + E + K[0] + W[j];
E = D;
D = C;
C = rotateLeft(B, 30);
B = A;
A = temp;

}
for (int j = 20; j < 40; j++) {
F = B ^ C ^ D;
// K = 0x6ED9EBA1;
temp = rotateLeft(A, 5) + F + E + K[1] + W[j];
E = D;
D = C;
C = rotateLeft(B, 30);
B = A;
A = temp;
}
for (int j = 40; j < 60; j++) {
F = (B & C) | (B & D) | (C & D);
// K = 0x8F1BBCDC;
temp = rotateLeft(A, 5) + F + E + K[2] + W[j];
E = D;
D = C;
C = rotateLeft(B, 30);
B = A;
A = temp;
}
for (int j = 60; j < 80; j++) {
F = B ^ C ^ D;
// K = 0xCA62C1D6;
temp = rotateLeft(A, 5) + F + E + K[3] + W[j];
E = D;
D = C;
C = rotateLeft(B, 30);
B = A;
A = temp;
}
H[0]
H[1]
H[2]
H[3]
H[4]

+=
+=
+=
+=
+=

A;
B;
C;
D;
E;

System.out.println("H0:"
System.out.println("H0:"
System.out.println("H0:"
System.out.println("H0:"
System.out.println("H0:"

+
+
+
+
+

Integer.toHexString(H[0]));
Integer.toHexString(H[1]));
Integer.toHexString(H[2]));
Integer.toHexString(H[3]));
Integer.toHexString(H[4]));

}
final int rotateLeft(int value, int bits) {

int q = (value << bits) | (value >>> (32 - bits));


return q;
}
}
private String intArrayToHexStr(int[] data) {
String output = "";
String tempStr = "";
int tempInt = 0;
for (int cnt = 0; cnt < data.length; cnt++) {
tempInt = data[cnt];
tempStr = Integer.toHexString(tempInt);
if (tempStr.length() == 1) {
tempStr = "0000000" + tempStr;
} else if (tempStr.length() == 2) {
tempStr = "000000" + tempStr;
} else if (tempStr.length() == 3) {
tempStr = "00000" + tempStr;
} else if (tempStr.length() == 4) {
tempStr = "0000" + tempStr;
} else if (tempStr.length() == 5) {
tempStr = "000" + tempStr;
} else if (tempStr.length() == 6) {
tempStr = "00" + tempStr;
} else if (tempStr.length() == 7) {
tempStr = "0" + tempStr;
}
output = output + tempStr;
}//end for loop
return output;
}//end intArrayToHexStr
//-------------------------------------------//
static final String toHexString(final ByteBuffer bb) {
final StringBuffer sb = new StringBuffer();
for (int i = 0; i < bb.limit(); i += 4) {
if (i % 4 == 0) {
sb.append('\n');
}
sb.append(toHexString(bb.getInt(i))).append(' ');
}
sb.append('\n');
return sb.toString();
}
static final String toHexString(int x) {
return padStr(Integer.toHexString(x));

}
static final String ZEROS = "00000000";
static final String padStr(String s) {
if (s.length() > 8) {
return s.substring(s.length() - 8);
}
return ZEROS.substring(s.length()) + s;
}
public void actionPerformed(String s) {
Digest digester = new Digest();
String z = s;
System.out.println("Message: " + z);
byte[] dataBuffer = (z).getBytes();
String thedigest = digester.digestIt(dataBuffer);
System.out.println("Output: " + thedigest);
}
public static void main(String[] args) {
SHA NgL = new SHA();
}
}

You might also like