MD5 's various implementations--and drunk.

Source: Internet
Author: User
Tags md5 digest

MD5 is message-digest algorithm 5 (Information-Digest algorithm 5), which is used to ensure complete and consistent information transmission. is one of the widely used hashing algorithms (also translation digest algorithm, hashing algorithm), mainstream programming language has been widely MD5 implemented.

--Baidu Encyclopedia

Of course, this article is not about what the principle of implementation, mainly to see how to achieve MD5 in different environments. MD5 encrypted is a byte array, but we generally take its hexadecimal string notation, of course, hexadecimal number string is case-sensitive, in MySQL database, Java, and JavaScript language, is generally used lowercase string to represent, and in The Oracle database is provided in a package that returns an uppercase string, which is considered a pit, and may need to be converted to lowercase if you want to perform multiple MD5.

1. mysql version MD5

MySQL implementation MD5 is the simplest, for what? Because he directly supports the MD5 function call.

Select MD5 (' 1 ') as M1, MD5 (MD5 (' 1 ')) as M2;
The execution results are:

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


2. Oracle version MD5

Note: In order 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 (32); 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 Dual2;


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


3. Java version MD5

The Java version is a bit more cumbersome, and this algorithm has a consistent comparison with the MD5 summary generated by MYSQLT.

Package Com.cncounter.util.common;import Java.security.messagedigest;import  java.security.nosuchalgorithmexception;/** * Java Message Digest algorithm MD5 tool class, in fact, other summary algorithm implementation is similar to */public class Md5util {/** * to text execution MD5   Digest encryption, this algorithm has a consistent comparison with the MD5 digest generated by Mysql,javascript.      * @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 = message      Digest.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 ("M    1= "+M1);  System.out.println ("m2=" +m2); }}

4. JavaScript version MD5 function

JavaScript implementation is the most troublesome, take a look below.

/*! The MD5 implementation of JavaScript implements a *///bracket expression, (XXXXX) is used to use the result of an internal statement, an expression as a result.//Common is to parse the JSON string with eval, need eval ("(" ("+jsonstr+"));//() The internal definition of a space, the definition of the variable does not pollute the global space, it is suitable for lib//(function UMD (object/Function name, context this, function/object definition)) return an anonymous function//because the first parenthesis results in a function, The function can be called as follows: (function (formal parameter) {}) (argument);//This anonymous function is automatically executed once by the browser parsing. (function UMD (name, context, definition) {if (typeof module!== "undefined" && module.exports) {//if module exists , and module.exports exists, the assignment result is assigned to it//can be used without tube Module.exports = definition (); } else if (typeof define = = = "function" && define.amd) {//If define this function exists, it should be another base class library, then define//can be used without tube define (definition); } else {///simply, you can call an incoming definition function to bind the returned object to the global space//Of course, depending on the context passed in, you can also bind to a different object underneath to become an attribute method. context[name] = Definition (name, context); }}) ("MD5", this, the function DEF (name, context) {"Use strict";//the use strict above represents a strict syntax pattern, and an error denies execution.//And Ordinary JS, is the interpretation of execution, not the place of execution, some The error also does not affect the execution of other code//as a class library, it is necessary to use strict mode. Strict mode declarations must be placed at the very beginning of a namespace space. var Old_public_api = (Context | | {}) [name]; MostThe object/function to return after. function Md5_func (text) {return hex_md5 (text);}; The following heap is a concrete algorithm ... Can not tube///////////////////////////////////////////////////////calculate MD5 var hexcase = 0; 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 = Arra Y (+), 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)), + f.length * 8); 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] |= << ((k)%), p[(((k + p) >>> 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 &GT;&G T 16); return (b << 16) | (C & 65535)}; function Bit_rol (A, B) {return (a << b) | (A >>> (32-b))}; Avoid global namespace Collisions Md5_func.noconflict = function publicapi$ Noconflict () {if(context) {//restore that name back to the original object context[name] = Old_public_api;} Return itself, held by the calling code itself, save, or assign a value to a variable return md5_func; }; The returned object, which is bound to a name such as MD5, references this object externally, and//Because of the closure feature, you can take advantage of the closure inner method and the inner object. Lib is generally the use of this closure characteristics, the maintenance of private properties, objects,//external only expose some methods (API), that is, function, the outside can only invoke these APIs, convenient internal logic control, reduce dependency. return md5_func;})

In short:

Their principles are the same, using the MD5 algorithm principle, can be implemented using a variety of computer languages, to form a variety of MD5 encryption verification tools. There are many online tools to do this, and these online tools are generally implemented in JavaScript and are very handy and quick to use.









MD5 's various implementations--and drunk.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.