The MD5
algorithm is a widely used hash function producing a 128-bit hash value.
Although MD5
was initially designed to be used as a cryptographic hash function,
it has been found to suffer from extensive vulnerabilities.
It can still be used as a checksum to verify data integrity, but only against unintentional corruption.
You can easily extend Informix with your own built in MD% function written in Java.
1 Source code
The following code provides the md5 hash of a string
import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; public class md5 { public static String getMD5(String text) throws NoSuchAlgorithmException { MessageDigest algorithm = MessageDigest.getInstance("MD5"); algorithm.reset(); byte[] data = text.trim().getBytes(); algorithm.update(data, 0, data.length); return __convert(algorithm.digest()); } /** * Convert a byte array into a printable format containing a * String of hexadecimal digit characters (two per byte). * * @param bytes Byte array representation * * public cause required by function_crypt_digest_sha ... */ private final static String __convert(byte bytes[]) { StringBuilder sb = new StringBuilder(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { sb.append(convertDigit((int) (bytes[i] >> 4))); sb.append(convertDigit((int) (bytes[i] & 0x0f))); } return (sb.toString()); } /** * [Private] Convert the specified value (0 .. 15) to the corresponding * hexadecimal digit. * * @param value Value to be converted */ private final static char convertDigit(int value) { value &= 0x0f; if (value >= 10) return ((char) (value - 10 + 'a')); // lowercase else return ((char) (value + '0')); } }
2 Compile
Compile the source code and pack the class into a jar file.
$ javac -source 1.7 -target 1.7 md5.java $ jar cvf md5.jar md5.class
3 Install
Execute the following SQL in the database where you want to install the routine.
BEGIN WORK; EXECUTE PROCEDURE sqlj.install_jar ("file:$INFORMIXDIR/extend/krakatoa/examples/md5.jar", "md5_jar", 0); CREATE FUNCTION MD5(lvarchar) RETURNS CHAR(32) EXTERNAL NAME 'md5_jar:md5.getMD5' LANGUAGE JAVA; GRANT EXECUTE ON FUNCTION MD5(lvarchar) TO ALL; COMMIT WORK;
To run install_jar
you need to have at least one Java virtual processor (JVP
).
If not, you will get the following error:
EXECUTE PROCEDURE sqlj.install_jar("file:$INFORMIXDIR/extend/krakatoa/examples/md5.jar", "md5_jar", 0); # ^ # 9799: User Defined Routine (install_jar) VP context switch failed. # #
To check available JVP
processors check onstat -g glo
for class jvp
4 Remove
- Drop the
MD5
functionCopyDROP FUNCTION MD5(lvarchar);
- Drop the
MD5
jarCopyEXECUTE PROCEDURE sqlj.remove_jar('md5_jar');
5 Update
To update the MD5
java UDR use
EXECUTE PROCEDURE sqlj.replace_jar ("file:$INFORMIXDIR/extend/krakatoa/examples/md5.jar", "md5_jar");
6 Testing
When testing we will get the following error as the security module is not installed by default in the IBM jre.
SELECT tabname[1,18], MD5(tabname) FROM systables ORDER BY 1;
Unknown throwable: (java.security.NoSuchAlgorithmException: MD5 MessageDigest not available)