📄 md5.java
字号:
/**************************************************************** * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * * * Copyright 2008 Jun Li(SiChuan University, the School of * * Software Engineering). All rights reserved. * * * * Licensed to the JMS under one or more contributor license * * agreements. See the LICENCE file distributed with this * * work for additional information regarding copyright * * ownership. The JMS licenses this file you may not use this * * file except in compliance with the License. * * * * Unless required by applicable law or agreed to in writing, * * software distributed under the License is distributed on an * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * * KIND, either express or implied. See the License for the * * specific language governing permissions and limitations * * under the License. * ****************************************************************/package org.jpxx.mail.Util.Security;import java.security.MessageDigest;/** * The algorithm takes as input a message of arbitrary length and produces * as output a 128-bit "fingerprint" or "message digest" of the input. * It is conjectured that it is computationally infeasible to produce * two messages having the same message digest, or to produce any * message having a given prespecified target message digest. The MD5 * algorithm is intended for digital signature applications, where a * large file must be "compressed" in a secure manner before being * encrypted with a private (secret) key under a public-key cryptosystem * such as RSA. * * The MD5 algorithm is designed to be quite fast on 32-bit machines. In * addition, the MD5 algorithm does not require any large substitution * tables; the algorithm can be coded quite compactly. * * The MD5 algorithm is an extension of the MD4 message-digest algorithm * 1,2]. MD5 is slightly slower than MD4, but is more "conservative" in * design. MD5 was designed because it was felt that MD4 was perhaps * being adopted for use more quickly than justified by the existing * critical review; because MD4 was designed to be exceptionally fast, * it is "at the edge" in terms of risking successful cryptanalytic * attack. MD5 backs off a bit, giving up a little in speed for a much * greater likelihood of ultimate security. It incorporates some * suggestions made by various reviewers, and contains additional * optimizations. The MD5 algorithm is being placed in the public domain * for review and possible adoption as a standard. * * * @author Jun Li * @version $Revision: 0.0.1 $, $Date: 2008/04/28 23:09:00 $ * @since JMS 0.0.1 */public class MD5 { /** * Encode String with MD5 * @param s String. * @return Encoded String */ public final static String encode(String s) { char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; try { byte[] strTemp = s.getBytes(); MessageDigest mdTemp = MessageDigest.getInstance("MD5"); mdTemp.update(strTemp); byte[] md = mdTemp.digest(); int j = md.length; char str[] = new char[j * 2]; int k = 0; for (int i = 0; i < j; i++) { byte byte0 = md[i]; str[k++] = hexDigits[byte0 >>> 4 & 0xf]; str[k++] = hexDigits[byte0 & 0xf]; } return new String(str); } catch (Exception e) { return null; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -