Thursday, March 31, 2011
SHA Alogorithm in Java
import java.security.MessageDigest;
public class Util {
public static String getSHA(String text)throws Exception{
MessageDigest md;
md = MessageDigest.getInstance("SHA-1");
md.update(text.getBytes("iso-8859-1"), 0, text.length());
return convertToHex(md.digest());
}
private static String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9))
buf.append((char) ('0' + halfbyte));
else
buf.append((char) ('a' + (halfbyte - 10)));
halfbyte = data[i] & 0x0F;
} while(two_halfs++ < 1);
}
return buf.toString();
}
public static void main(String[] args) throws Exception {
System.out.println(Util.getSHA("Vinay Guntaka"));
}
}
public class Util {
public static String getSHA(String text)throws Exception{
MessageDigest md;
md = MessageDigest.getInstance("SHA-1");
md.update(text.getBytes("iso-8859-1"), 0, text.length());
return convertToHex(md.digest());
}
private static String convertToHex(byte[] data) {
StringBuffer buf = new StringBuffer();
for (int i = 0; i < data.length; i++) {
int halfbyte = (data[i] >>> 4) & 0x0F;
int two_halfs = 0;
do {
if ((0 <= halfbyte) && (halfbyte <= 9))
buf.append((char) ('0' + halfbyte));
else
buf.append((char) ('a' + (halfbyte - 10)));
halfbyte = data[i] & 0x0F;
} while(two_halfs++ < 1);
}
return buf.toString();
}
public static void main(String[] args) throws Exception {
System.out.println(Util.getSHA("Vinay Guntaka"));
}
}
Generating Unique Number in Java
import java.util.Random;
public class UniqueIDGenerator {
private static final long serialVersionUID = 1L;
/**
*
* Random-number generator.
*/
private static transient Random r = new Random();
// use constanst to prevent warnings about magic numbers:
private static final long HEX_3FFFFFFFFFFFFFFF = 0x3FFFFFFFFFFFFFFFL;
private static final long HEX_8000000000000000 = 0x8000000000000000L;
private static final long HEX_FFFFFFFFFFFF0FFF = 0xFFFFFFFFFFFF0FFFL;
private static final long HEX_4000 = 0x4000;
private static final long HEX_FFFFFFFF00000000 = 0xFFFFFFFF00000000L;
private static final long HEX_FFFFFFFF = 0xFFFFFFFFL;
private static final long HEX_FFFF0000 = 0xFFFF0000L;
private static final long HEX_0000000000000000FFFF = 0x0000000000000000FFFFL;
private static final long HEX_FFFF000000000000 = 0xFFFF000000000000L;
private static final long HEX_FFFF = 0xFFFF;
private static final long HEX_FFFFFFFFFFFF = 0xFFFFFFFFFFFFL;
private static final int DEC_4 = 4;
private static final int DEC_8 = 8;
private static final int DEC_12 = 12;
private static final int DEC_16 = 16;
private static final int DEC_32 = 32;
private static final int DEC_48 = 48;
/**
*
* @return a string with a universally unique id of format:
*
* xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx .
*/
public static String generateUUID() {
// calculate lsb and msb
long leastSigBits = r.nextLong();
long mostSigBits = r.nextLong();
leastSigBits &= HEX_3FFFFFFFFFFFFFFF;
leastSigBits |= HEX_8000000000000000; // set top two bits to variant 2
mostSigBits &= HEX_FFFFFFFFFFFF0FFF;
mostSigBits |= HEX_4000; // Version 4;
// turn leastSigBits and mostSigBits into string of format:
// xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
String id =
padHex(((mostSigBits & HEX_FFFFFFFF00000000) >> DEC_32) & HEX_FFFFFFFF,
DEC_8)
+ "-"
+ padHex(((mostSigBits & HEX_FFFF0000) >> DEC_16), DEC_4)
+ "-"
+ padHex((mostSigBits & HEX_0000000000000000FFFF), DEC_4)
+ "-"
+ padHex(
(((leastSigBits & HEX_FFFF000000000000) >> DEC_48) & HEX_FFFF),
DEC_4)
+ "-"
+ padHex(leastSigBits & HEX_FFFFFFFFFFFF, DEC_12);
return id;
}
/**
*
* Returns a hex String from l, padded to n spaces.
*
* @param l
* .
*
* @param n
* .
*
* @return String
*/
private static String padHex(long l, int n) {
String s = Long.toHexString(l);
while (s.length() < n) {
s = "0" + s;
}
return s;
}
public static void main(String[] args) {
System.out.println(generateUUID());
}
}
Subscribe to:
Posts (Atom)
Java 1.7 New Features Over 1.6
Automatic Resource Management Description: A proposal to support scoping of resource usage in a block with automatic resource cleanup. T...
-
Differences between Struts and Springs Springs: · Dependency Injection · Inversion of Control · Transaction ...
-
Creating a custom list view – a custom layout, a custom row layout and how we bind the custom data holder to these layouts. So, here we ...
-
Hello, Retrieving multiple result sets and OUT Parameters by passing IN Parameter from a stored procedure in a JDBC application. Connect...