public class Register
{
private static final String smcode = "0123456789ABCDEF";
public static String createRegCode(String mcode)
{
if (mcode == null)
return null;
mcode = mcode.trim().toUpperCase().replace("-", "");
int len = mcode.length();
if (len != 25)
return null;
for (int i = 0; i < len; ++i) {
char c = mcode.charAt(i);
if ("0123456789ABCDEF".indexOf(c) == -1)
return null;
}
String rcode = createCode(mcode);
String srcode = splitRCode(rcode);
if (srcode != null)
return srcode;
return null;
}
private static String md5(String str) {
try {
md5 = MessageDigest.getInstance("MD5");
return bytesToHex(md5.digest(str.getBytes())).toUpperCase();
} catch (java.lang.Exception md5) {
}
return null; }
private static String bytesToHex(byte[] bytes) {
StringBuffer md5str = new StringBuffer();
for (int i = 0; i < bytes.length; ++i) {
int digital = bytes[i];
if (digital < 0)
digital += 256;
if (digital < 16)
md5str.append("0");
md5str.append(Integer.toHexString(digital));
}
return md5str.toString().toUpperCase();
}
private static String createCode(String mcode)
{
mcode = mcode.replace("-", "");
char chr = mcode.charAt(20);
mcode = mcode.toUpperCase().substring(0, 20);
String str = md5(mcode);
String rcode = str.substring(0, 20);
rcode = rcode + chr;
rcode = rcode + genExtCode(str);
return rcode;
}
private static String splitRCode(String rcode)
{
StringBuffer buffer = new StringBuffer();
int len = rcode.length();
for (int i = 0; i < len; i += 5) {
if (buffer.length() > 0)
buffer.append("-");
buffer.append(rcode.substring(i, i + 5));
}
return buffer.toString();
}
private static String genExtCode(String mcode)
{
StringBuffer buffer = new StringBuffer();
for (int i = 3; i >= 0; --i)
buffer.append(mcode.charAt(i * 5));
return buffer.toString();
}
}