Java, JavaScript, Oracle, MySQL implementation of the MD5 encryption algorithm sharing _java

Source: Internet
Author: User
Tags closure eval md5 md5 digest md5 encryption mysql version

MD5, all known as Message Digest algorithm 5 (the fifth edition of the Messaging Digest algorithm). For more information, please refer to Wikipedia: MD5

MD5 is encrypted with a byte array, but we typically take its hexadecimal string notation, and of course, the hexadecimal string is case-sensitive, in the MySQL database, Java, and JavaScript languages, typically using lowercase strings to represent the Oracle database in the official package, the return of the uppercase string, which is a pit, if you want to perform multiple MD5, you may need to convert to lowercase.

The relevant code is as follows:

1. Java version MD5

Md5util.java

Package Com.cncounter.util.common;
Import Java.security.MessageDigest;

Import java.security.NoSuchAlgorithmException; /** * Java Message Digest algorithm MD5 tool class, in fact, other digest algorithm implementation is similar to * * public class Md5util {/** * to text Execution MD5 digest encryption, this algorithm and Mysql,javascript generated MD5 summary
  A consistent comparison has been made.
   * @param plaintext * @return The letter in the return value is lowercase */public static String MD5 (string plaintext) {if (null = = plaintext) {
  plaintext = "";
  } String md5str = ""; try {//JDK 6 supports the following 6 message digest algorithms, case-insensitive//Md5,sha (sha-1), md2,sha-256,sha-384,sha-512 messagedigest MD = messagedigest
   . getinstance ("MD5");
   Md.update (Plaintext.getbytes ());

   byte b[] = Md.digest ();

   int i;
   StringBuilder builder = new StringBuilder (32);
    for (int offset = 0; offset < b.length; offset++) {i = B[offset];
    if (i < 0) i = 256;
    if (I <) builder.append ("0");
   Builder.append (integer.tohexstring (i));
   } md5str = Builder.tostring (); LOGUTIL.PRINTLN ("Result:" + buf.tostring ());//32-bit encryption} catch (NoSuchalgorithmexception e) {e.printstacktrace ();
 return md5str;
  //A simple version of the test public static void main (string[] args) {String m1 = MD5 ("1");
  String m2 = MD5 (m1); /* Output is * m1=c4ca4238a0b923820dcc509a6f75849b * m2=28c8edde3d61a0411511d3b1866f0636 * * SYSTEM.OUT.PRINTLN ("m1=" +
  M1);
 System.out.println ("m2=" +m2);

 }
}

2. mysql version MD5

MySQL directly supports MD5 function calls

Select MD5 (' 1 ') as M1, MD5 (MD5 (' 1 ')) as M2;

The results of the execution are:

MARIADB [(None)]> Select MD5 (' 1 ') as M1, MD5 (MD5 (' 1 ')) as M2;
+----------------------------------+----------------------------------+
| m1 |        m2 |
+----------------------------------+----------------------------------+
| c4ca4238a0b923820dcc509a6f75849b | 28c8edde3d61a0411511d3b1866f0636 |
+----------------------------------+----------------------------------+
1 row in Set (0.00 sec)

3. JavaScript version MD5 function

The Md5.js code is as follows:

/*!
The MD5 implementation of JAVASCRIPT//bracket Expressions (XXXXX) is used to make internal statements, the results of expressions as a result.
It is common for eval ("+jsonstr+") to be required when parsing a JSON string with eval ("(")). () internally defines a space, the variables defined inside do not contaminate the global space and are ideal for lib//(function UMD (object/Function name, context this, function/object definition)) returns an anonymous function//Because the result in the first parenthesis is a function, and
The function can be invoked like this: (function (formal parameter) {}) (argument);
This anonymous function is automatically executed once after it is parsed by the browser. (function UMD (name, context, definition) {if (typeof module!== "undefined" && module.exports) {//If module
 exists, and the module.exports exists, the assignment result is assigned to it//can be used without module.exports = definition (); else if (typeof define = = "function" && define.amd) {//If define this function exists, it should be another base class library, then use define//You can do without the Def
 INE (definition);
 else {//simple, you can see: Call the incoming definition function, bind the returned object to the global space//And of course, depending on the context passed in, you can also bind to the other object below as a property method.
 Context[name] = definition (name, context);
 }) ("MD5", this, function DEF (name, context) {' Use strict ';
 The use strict above represents a strict syntax pattern and refuses to execute if there are errors.

 And ordinary JS, is the interpretation of execution, do not execute the place, and some errors do not affect the execution of other code as a class library, the use of strict mode is very necessary. Strict schema declarations must be placed at the beginning of a namespace space. var Old_public_api = (Context | |
 {}) [name];
 The last object/function to return.
 function Md5_func (text) {return hex_md5 (text);
 }; Here's a bunch of specific algorithms ...
 You can calculate MD5 var hexcase = 0 without///////////////////////////////////////////////////////;
 function Hex_md5 (a) {if (A = = "") return A;
 Return Rstr2hex (RSTR_MD5 (Str2rstr_utf8 (A)))};
 function Hex_hmac_md5 (A, b) {return Rstr2hex (RSTR_HMAC_MD5 (Str2rstr_utf8 (a), Str2rstr_utf8 (b))};
 function Md5_vm_test () {return HEX_MD5 ("abc"). toLowerCase () = = "900150983cd24fb0d6963f7d28e17f72"};
 function Rstr_md5 (a) {return binl2rstr (BINL_MD5 (Rstr2binl (a), A.length * 8))};
 function Rstr_hmac_md5 (c, f) {var E = Rstr2binl (c);
 if (E.length >) {e = Binl_md5 (E, C.length * 8)} var a = array, d = Array (16);
 for (var b = 0; b < b++) {A[b] = e[b] ^ 909522486;
 D[B] = e[b] ^ 1549556828} var g = Binl_md5 (A.concat (Rstr2binl (f)), 8 + f.length *);
 Return Binl2rstr (Binl_md5 (D.concat (g), 512 + 128))}; function Rstr2hex (c) {try {hexcase} catch (g) {hexcase = 0} var f = hexcase?
 "0123456789ABCDEF": "0123456789abcdef";
 var B = "";
 var A;
 for (var d = 0; d < c.length; d++) {a = C.charcodeat (d);
 B + + F.charat ((a >>> 4) &) + F.charat (A &)} return B};
 function Str2rstr_utf8 (c) {var b = "";
 var d =-1;
 var a, E;
 while (++d < c.length) {a = C.charcodeat (d); E = d + 1 < c.length?
 C.charcodeat (d + 1): 0; if (55296 <= a && a <= 56319 && 56320 <= e && e <= 57343) {a = 65536 + ((A & 102
 3) << + (E & 1023); d++} if (a <= 127) {b + = String.fromCharCode (a)} else {if (a <= 2047) {b = String.fromCharCode (192 | ((a >>> 6) & 31), 128 |
 (A & 63)) else {if (a <= 65535) {b + = String.fromCharCode (224 | ((a >>>) & 15), 128 | ((a >>> 6) & 63), 128 |
  (A & 63)) else {if (a <= 2097151) {b + = String.fromCharCode (240 | ((a >>>) & 7), 128 | ((a >>>) & 63), 128 | ((a >>> 6) & 63), 128 |
  (A & 63))
 }}} return B};
 function Rstr2binl (b) {var a = Array (b.length >> 2); for (var c = 0; c < a.length; C + +) {A[c] = 0} for (var c = 0; c < b.length * 8; c = 8) {a[c >> 5] |= (
 B.charcodeat (C/8) & 255) << (c)} return a};
 function Binl2rstr (b) {var a = ""; for (var c = 0; c < b.length * C + + 8) {A + = String.fromCharCode ((b[c >> 5] >>> (c)) & 25
 5)} return a};
 function Binl_md5 (p, k) {p[k >> 5] |= 128 << ((k)% 32);
 P[(((k +) >>> 9) << 4) + +] = k;
 var o = 1732584193;
 var n =-271733879;
 var m =-1732584194;
 var L = 271733878;
 for (var g = 0; g < p.length; g = =) {var j = o;
 var h = n;
 var f = m;
 var e = l;
 o = md5_ff (o, N, M, L, P[g + 0], 7,-680876936);
 L = md5_ff (l, O, N, M, P[g + 1], 12,-389564586); m = md5_ff (M, L, O, N, p[g + 2], 17, 606105819);
 n = md5_ff (n, M, L, O, P[g + 3], 22,-1044525330);
 o = md5_ff (o, N, M, L, P[g + 4], 7,-176418897);
 L = md5_ff (l, O, N, M, P[g + 5], 12, 1200080426);
 m = md5_ff (M, L, O, N, p[g + 6], 17,-1473231341);
 n = md5_ff (n, M, L, O, P[g + 7], 22,-45705983);
 o = md5_ff (o, N, M, L, P[g + 8], 7, 1770035416);
 L = md5_ff (l, O, N, M, P[g + 9], 12,-1958414417);
 m = md5_ff (M, L, O, N, p[g + 10], 17,-42063);
 n = md5_ff (n, M, L, O, P[g + 11], 22,-1990404162);
 o = md5_ff (o, N, M, L, P[g + 12], 7, 1804603682);
 L = md5_ff (l, O, N, m, P[g + 13], 12,-40341101);
 m = md5_ff (M, L, O, N, p[g + 14], 17,-1502002290);
 n = md5_ff (n, M, L, O, P[g + 15], 22, 1236535329);
 o = Md5_gg (o, N, M, L, P[g + 1], 5,-165796510);
 L = Md5_gg (l, O, N, M, P[g + 6], 9,-1069501632);
 m = Md5_gg (M, L, O, N, p[g + 11], 14, 643717713);
 n = Md5_gg (n, M, L, O, P[g + 0], 20,-373897302);
 o = Md5_gg (o, N, M, L, P[g + 5], 5,-701558691);
 L = Md5_gg (l, O, N, m, P[g + 10], 9, 38016083); m = Md5_gg (M, L, O, N, P[g + 15], 14,-660478335);
 n = Md5_gg (n, M, L, O, P[g + 4], 20,-405537848);
 o = Md5_gg (o, N, M, L, P[g + 9], 5, 568446438);
 L = Md5_gg (l, O, N, M, P[g + 14], 9,-1019803690);
 m = Md5_gg (M, L, O, N, p[g + 3], 14,-187363961);
 n = Md5_gg (n, M, L, O, P[g + 8], 20, 1163531501);
 o = Md5_gg (o, N, M, L, P[g + 13], 5,-1444681467);
 L = Md5_gg (l, O, N, M, P[g + 2], 9,-51403784);
 m = Md5_gg (M, L, O, N, p[g + 7], 14, 1735328473);
 n = Md5_gg (n, M, L, O, P[g + 12], 20,-1926607734);
 o = md5_hh (o, N, M, L, P[g + 5], 4,-378558);
 L = md5_hh (l, O, N, M, P[g + 8], 11,-2022574463);
 m = md5_hh (M, L, O, N, p[g + 11], 16, 1839030562);
 n = md5_hh (n, M, L, O, P[g + 14], 23,-35309556);
 o = md5_hh (o, N, M, L, P[g + 1], 4,-1530992060);
 L = md5_hh (l, O, N, M, P[g + 4], 11, 1272893353);
 m = md5_hh (M, L, O, N, p[g + 7], 16,-155497632);
 n = md5_hh (n, M, L, O, P[g + 10], 23,-1094730640);
 o = md5_hh (o, N, M, L, P[g + 13], 4, 681279174);
L = md5_hh (l, O, N, m, p[g + 0], 11,-358537222); m = md5_hh (M, L, O, N, p[g + 3], 16,-722521979);
 n = md5_hh (n, M, L, O, P[g + 6], 23, 76029189);
 o = md5_hh (o, N, M, L, P[g + 9], 4,-640364487);
 L = md5_hh (l, O, N, M, P[g + 12], 11,-421815835);
 m = md5_hh (M, L, O, N, p[g + 15], 16, 530742520);
 n = md5_hh (n, M, L, O, P[g + 2], 23,-995338651);
 o = Md5_ii (o, N, M, L, P[g + 0], 6,-198630844);
 L = Md5_ii (l, O, N, M, P[g + 7], 10, 1126891415);
 m = Md5_ii (M, L, O, N, p[g + 14], 15,-1416354905);
 n = Md5_ii (n, M, L, O, P[g + 5], 21,-57434055);
 o = Md5_ii (o, N, M, L, P[g + 12], 6, 1700485571);
 L = Md5_ii (l, O, N, M, P[g + 3], 10,-1894986606);
 m = Md5_ii (M, L, O, N, p[g + 10], 15,-1051523);
 n = Md5_ii (n, M, L, O, P[g + 1], 21,-2054922799);
 o = Md5_ii (o, N, M, L, P[g + 8], 6, 1873313359);
 L = Md5_ii (l, O, N, M, P[g + 15], 10,-30611744);
 m = Md5_ii (M, L, O, N, p[g + 6], 15,-1560198380);
 n = Md5_ii (n, M, L, O, P[g + 13], 21, 1309151649);
 o = Md5_ii (o, N, M, L, P[g + 4], 6,-145523070); L = Md5_ii (l, O, N, m, P[g + 11], 10,-1120210379);
 m = Md5_ii (M, L, O, N, p[g + 2], 15, 718787259);
 n = Md5_ii (n, M, L, O, P[g + 9], 21,-343485551);
 o = Safe_add (o, j);
 n = Safe_add (n, h);
 m = Safe_add (M, f);
 L = Safe_add (L, E)} return Array (O, N, M, L)};
 function md5_cmn (h, E, D, C, G, f) {return Safe_add (Bit_rol (Safe_add (Safe_add (E, h), Safe_add (c, f), G), D)}; function md5_ff (g, F, K, J, E, I, h) {return md5_cmn (F & k) |
 ((~f) & J), G, F, E, I, h)}; function Md5_gg (g, F, K, J, E, I, h) {return md5_cmn (F & j) |
 (K & (~j)), G, F, E, I, h)};
 function md5_hh (g, F, K, J, E, I, h) {return md5_cmn (f ^ k ^ J, G, F, E, I, h)}; function Md5_ii (g, F, K, J, E, I, h) {return md5_cmn k ^ (f |
 (~j)), G, F, E, I, h)};
 function Safe_add (A, D) {var c = (A & 65535) + (d & 65535);
 var B = (a >>) + (d >>) + (c >> 16); return (b << 16) |
 (C & 65535)}; function Bit_rol (A, B) {return (a << b) | (A >>> (32-b))};

 Avoid global namespace conflicts md5_func.noconflict = function PUBLICAPI$NOCONFL
 ICT () {if (context) {//restore that name back to the original object context[name] = Old_public_api;
 //returns itself, held by the calling code itself, saved, or assigned to a variable return md5_func;

 }; The returned object is bound to a name such as MD5, which is referenced externally,//Because of the closure attribute, the closure internal methods and internal objects can be exploited.
 Lib generally uses this closure feature to maintain private properties, objects,//external only exposes some methods (APIs), that is, function, outside can only invoke these APIs, facilitate internal logic control, reduce dependency.
return md5_func;

 })

Test results:

MD5 ("1");
" c4ca4238a0b923820dcc509a6f75849b "
MD5 (MD5 (" 1 "))
" 28c8edde3d61a0411511d3b1866f0636 "

4. Oracle Database Edition MD5

Note: To be consistent with the MD5 method in other environments, you need to convert to lowercase
You need to create a storage function:

CREATE OR REPLACE FUNCTION MD5 (passwd in VARCHAR2) return VARCHAR2 is
 retval varchar2 (a);
BEGIN
 retval: = Lower (Utl_raw.cast_to_raw (
dbms_obfuscation_toolkit. MD5 (input_string => passwd))
);
 return retval;
End;

Test results:

Sql> Select MD5 (' 1 ') as M1, MD5 (MD5 (' 1 ')) as M2 from dual
 2;

M1
---------------------------------------
M2
---------------------------------------
c4ca4238a0b923820dcc509a6f75849b
28c8edde3d61a0411511d3b1866f0636

PS: Here again for you to provide 2 MD5 encryption tools, interested friends can refer to:

MD5 Online Encryption Tool:

Http://tools.jb51.net/password/CreateMD5Password

Online md5/hash/sha-1/sha-2/sha-256/sha-512/sha-3/ripemd-160 Encryption Tool:

Http://tools.jb51.net/password/hash_md5_sha

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.