Common MD5 algorithm code

Source: Internet
Author: User
Tags define function md5 digest
Common MD5 algorithm code
Date: January 1, August 4, 2014
Author: Tie

MD5, all called message digest algorithm 5 (Message Digest algorithm Fifth Edition). For more information, see Wikipedia: MD5 

After MD5 encryption, it is a byte array, but we generally use its hexadecimal string notation. Of course, the hexadecimal string is case sensitive. in MySQL database, Java, and JavaScript are generally represented by lowercase strings, while in the pack officially provided by the Oracle database, the returned strings are uppercase strings, which is a pitfall, if you want to execute MD5 multiple times, you may need to convert it to lower case.


PleaseClick to enter the source code zip file download page.

The related 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, the implementation of other digest algorithms is similar to */public class md5util, this algorithm is consistent with the MD5 Digest generated by MySQL and JavaScript. * @ Param plaintext * @ return the return value contains lowercase letters */public static string MD5 (string plaintext) {If (null = plaintext) {plaintext = "";} string md5str = ""; try {// JDK 6 supports the following 6 message digest algorithms, case-insensitive, 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 <16) 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 lite version of the test public static void main (string [] ARGs) {string M1 = MD5 ("1 "); string m2 = MD5 (M1);/* the 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 execution result is:
MariaDB [(none)]> select md5(‘1‘) as m1, md5(md5(‘1‘)) as m2;+----------------------------------+----------------------------------+| m1                               | m2                               |+----------------------------------+----------------------------------+| c4ca4238a0b923820dcc509a6f75849b | 28c8edde3d61a0411511d3b1866f0636 |+----------------------------------+----------------------------------+1 row in set (0.00 sec)

3. MD5 functions for Javascript

The md5.js code is as follows:
/*! The MD5 Implementation of JavaScript * // parentheses expression. (XXXXX) is used to take the results of internal statements and expressions as a result. // when parsing JSON strings with Eval, Eval ("(" + jsonstr + ")"); // () internally defines a space, the variables defined in it will not pollute the global space. It is suitable for lib // (function UMD (Object/function name, context this, function/object definition )) returns an anonymous function // because the result in the first parentheses is a function, and the function can be called like this: (function (form parameter) {}) (real parameter ); // this anonymous function is automatically executed once it is parsed by the browser. (function UMD (name, context, definition) {If (typeof module! = "Undefined" & module. exports) {// If the module exists, and the module. if exports exists, assign the value assignment result to it // you do not need to worry about module. exports = Definition ();} else if (typeof define = "function" & define. amd) {// If the define function exists, it should be another basic class library, then use define // to skip define (Definition);} else {// a little simpler, you can think of it as: Call the passed definition function to bind the returned object to the global space // Of course, depending on the input context, you can also bind it to another object to become an attribute method. context [name] = Definition (name, context) ;}) ("MD5", this, Function Def (name, context) {"use strict"; // The use strict above indicates the strict syntax mode. If an error occurs, the execution is denied. // while common JS is where the execution is interpreted and not executed, and some errors do not affect the execution of other codes. // as a class library, strict mode is necessary. strict mode Declaration must be placed at the very beginning of a namespace. // var old_public_api = (context | |{}) [name]; // The final object to be returned/function. function md5_func (text) {return hex_md5 (text) ;}; // The following is a specific algorithm... you can skip this step first //////////////////////////////////// ///////////////// calculate md5var hexcase = 0; function hex_md5 (a) {if (a = "") Re Turn a; return rstr2hex (rstr_md5 (str2rstr_utf8 (A)}; function hex_hmac_md5 (a, B) {return rstr2hex (rstr_hmac_md5 (str2rstr_utf8 (), str2rstr_utf8 (B)}; function md5_vm_test () {return hex_md5 ("ABC "). tolowercase () = "900150983cd24fb0d6963f7d28e17f72"}; function rstr_md5 (a) {return binl2rstr (binl_md5 (rstr2binl (A),. length * 8)}; function rstr_hmac_md5 (C, F) {var E = rstr2binl (c); If (E. length> 16) {e = Binl_md5 (E, C. length * 8)} var A = array (16), D = array (16); For (VAR B = 0; B <16; B ++) {A [B] = E [B] ^ 909522486; d [B] = E [B] ^ 1549556828} var G = binl_md5 (. concat (rstr2binl (f), 512 + F. length * 8); Return binl2rstr (binl_md5 (D. concat (G), 512 + 128)}; function rstr2hex (c) {try {hexcase} catch (g) {hexcase = 0} var F = hexcase? "0123456789 abcdef": "0123456789 abcdef"; var B = ""; var A; For (VAR D = 0; D <C. length; D ++) {A = C. charcodeat (d); B + = f. charat (A >>> 4) & 15) + F. charat (A & 15)} 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 & 1023) <10) + (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 >>> 12) & 15), 128 | (A >>> 6) & 63), 128 | (A & 63 ))} else {if (a <= 2097151) {B + = string. fromcharcode (240 | (A >>> 18) & 7), 128 | (A >>> 12) & 63), 128 | (A >>> 6) & 63), 128 | (A & 63) }}} return B}; function rstr2binl (B) {var A = array (B. length> 2); For (var c = 0; C <. length; C ++) {A [c] = 0} For (var c = 0; C <B. length * 8; C ++ = 8) {A [C> 5] | = (B. charcodeat (C/8) & 255) <(C % 32)} return a}; function binl2rstr (B) {var A = ""; for (VAR C = 0; c <B. length * 32; C + = 8) {A + = string. fromcharcode (B [C> 5] >>>( C % 32) & 255)} return a}; function binl_md5 (P, K) {P [k> 5] | = 128 <(k) % 32); P [(K + 64)> 9) <4) + 14] = K; var o = 1732584193; var n =-271733879; var M =-1732584194; var L = 271733878; For (var g = 0; G <p. length; G + = 16) {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,606 105819); 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,120 0080426); 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,123 6538529 ); O = md5_gg (O, N, M, L, P [G + 1], 5,-165796510); L = md5_gg (L, O, N, m, P [G + 6], 9,-10695. 01632); M = md5_gg (M, L, O, N, P [G + 11], 14,643 717713); 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 = maid (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,116 running 1501); 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,173 5328473); 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,183 9030133); 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,127 2893353); 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,760 29189 ); 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,530 742520 ); 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,112 6891415 ); 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,130 9151649); 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,718 787259); 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_cen (H, E, D, C, G, F) {retu Rn safe_add (bit_rol (safe_add (E, h), safe_add (C, F), g), d)}; function md5_ff (G, F, K, J, e, I, h) {return md5_cen (F & K) | ((~ F) & J), G, F, E, I, h)}; function md5_gg (G, F, K, J, E, I, H) {return md5_cen (F & J) | (K &(~ J), G, F, E, I, h)}; function md5_hh (G, F, K, J, E, I, H) {return md5_cen (f ^ K ^ J, G, F, E, I, h)}; function md5_ii (G, F, K, J, E, I, H) {return md5_cen (K ^ (f | (~ J), G, F, E, I, h)}; function safe_add (a, d) {var c = (A & 65535) + (D & 65535 ); vaR B = (A> 16) + (D> 16) + (C> 16); Return (B <16) | (C & 65535 )}; function bit_rol (a, B) {return (A <B) | (A >>> (32-B ))}; //////////////////////////////////////// //// // avoid global namespace conflict md5_func.noconflict = function publicapi $ noconflict () {If (context) {// restore the name to the original object context [name] = old_public_api ;}/ /Return itself, held by the called code, saved, or assigned to a variable return md5_func ;}; // The returned object will be bound to names such as MD5, this object is referenced externally. // because of the closure feature, internal methods and internal objects of the closure can be used. Lib generally uses this closure feature to maintain private attributes and objects. // only some methods (APIS), that is, functions, can be called outside, it facilitates internal logic control and reduces dependencies. return md5_func ;})

Test results:
md5("1");"c4ca4238a0b923820dcc509a6f75849b"md5(md5("1"))"28c8edde3d61a0411511d3b1866f0636"

4. Oracle database version MD5

Note: To be consistent with the MD5 method in other environments, it must be converted to lowercase letters.
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 dual  2  ;M1---------------------------------------M2---------------------------------------c4ca4238a0b923820dcc509a6f75849b28c8edde3d61a0411511d3b1866f0636
For relevant source code, click to enter the download page. For other languages, search Corresponding language, MD5.

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.