Java--我即几的MD5加密
Posted 左百工
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java--我即几的MD5加密相关的知识,希望对你有一定的参考价值。
百度的时候发现有人无耻的用了写好的工具类,那我就更无耻了。。。我也用。
我是在:org.apache.commons.codec.digest.DigestUtils 这个类里面找到的一个工具
1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.codec.digest; 19 20 import java.io.IOException; 21 import java.io.InputStream; 22 import java.security.MessageDigest; 23 import java.security.NoSuchAlgorithmException; 24 25 import org.apache.commons.codec.binary.Hex; 26 import org.apache.commons.codec.binary.StringUtils; 27 28 /** 29 * Operations to simplify common {@link java.security.MessageDigest} tasks. 30 * This class is immutable and thread-safe. 31 * 32 * @version $Id: DigestUtils.java 1634433 2014-10-27 01:10:47Z ggregory $ 33 */ 34 public class DigestUtils { 35 36 private static final int STREAM_BUFFER_LENGTH = 1024; 37 38 /** 39 * Read through an InputStream and returns the digest for the data 40 * 41 * @param digest 42 * The MessageDigest to use (e.g. MD5) 43 * @param data 44 * Data to digest 45 * @return the digest 46 * @throws IOException 47 * On error reading from the stream 48 */ 49 private static byte[] digest(final MessageDigest digest, final InputStream data) throws IOException { 50 return updateDigest(digest, data).digest(); 51 } 52 53 /** 54 * Returns a <code>MessageDigest</code> for the given <code>algorithm</code>. 55 * 56 * @param algorithm 57 * the name of the algorithm requested. See <a 58 * href="http://docs.oracle.com/javase/6/docs/technotes/guides/security/crypto/CryptoSpec.html#AppA" 59 * >Appendix A in the Java Cryptography Architecture Reference Guide</a> for information about standard 60 * algorithm names. 61 * @return A digest instance. 62 * @see MessageDigest#getInstance(String) 63 * @throws IllegalArgumentException 64 * when a {@link NoSuchAlgorithmException} is caught. 65 */ 66 public static MessageDigest getDigest(final String algorithm) { 67 try { 68 return MessageDigest.getInstance(algorithm); 69 } catch (final NoSuchAlgorithmException e) { 70 throw new IllegalArgumentException(e); 71 } 72 } 73 74 /** 75 * Returns an MD2 MessageDigest. 76 * 77 * @return An MD2 digest instance. 78 * @throws IllegalArgumentException 79 * when a {@link NoSuchAlgorithmException} is caught, which should never happen because MD2 is a 80 * built-in algorithm 81 * @see MessageDigestAlgorithms#MD2 82 * @since 1.7 83 */ 84 public static MessageDigest getMd2Digest() { 85 return getDigest(MessageDigestAlgorithms.MD2); 86 } 87 88 /** 89 * Returns an MD5 MessageDigest. 90 * 91 * @return An MD5 digest instance. 92 * @throws IllegalArgumentException 93 * when a {@link NoSuchAlgorithmException} is caught, which should never happen because MD5 is a 94 * built-in algorithm 95 * @see MessageDigestAlgorithms#MD5 96 */ 97 public static MessageDigest getMd5Digest() { 98 return getDigest(MessageDigestAlgorithms.MD5); 99 } 100 101 /** 102 * Returns an SHA-1 digest. 103 * 104 * @return An SHA-1 digest instance. 105 * @throws IllegalArgumentException 106 * when a {@link NoSuchAlgorithmException} is caught, which should never happen because SHA-1 is a 107 * built-in algorithm 108 * @see MessageDigestAlgorithms#SHA_1 109 * @since 1.7 110 */ 111 public static MessageDigest getSha1Digest() { 112 return getDigest(MessageDigestAlgorithms.SHA_1); 113 } 114 115 /** 116 * Returns an SHA-256 digest. 117 * <p> 118 * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0. 119 * </p> 120 * 121 * @return An SHA-256 digest instance. 122 * @throws IllegalArgumentException 123 * when a {@link NoSuchAlgorithmException} is caught, which should never happen because SHA-256 is a 124 * built-in algorithm 125 * @see MessageDigestAlgorithms#SHA_256 126 */ 127 public static MessageDigest getSha256Digest() { 128 return getDigest(MessageDigestAlgorithms.SHA_256); 129 } 130 131 /** 132 * Returns an SHA-384 digest. 133 * <p> 134 * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0. 135 * </p> 136 * 137 * @return An SHA-384 digest instance. 138 * @throws IllegalArgumentException 139 * when a {@link NoSuchAlgorithmException} is caught, which should never happen because SHA-384 is a 140 * built-in algorithm 141 * @see MessageDigestAlgorithms#SHA_384 142 */ 143 public static MessageDigest getSha384Digest() { 144 return getDigest(MessageDigestAlgorithms.SHA_384); 145 } 146 147 /** 148 * Returns an SHA-512 digest. 149 * <p> 150 * Throws a <code>RuntimeException</code> on JRE versions prior to 1.4.0. 151 * </p> 152 * 153 * @return An SHA-512 digest instance. 154 * @throws IllegalArgumentException 155 * when a {@link NoSuchAlgorithmException} is caught, which should never happen because SHA-512 is a 156 * built-in algorithm 157 * @see MessageDigestAlgorithms#SHA_512 158 */ 159 public static MessageDigest getSha512Digest() { 160 return getDigest(MessageDigestAlgorithms.SHA_512); 161 } 162 163 /** 164 * Returns an SHA-1 digest. 165 * 166 * @return An SHA-1 digest instance. 167 * @throws IllegalArgumentException 168 * when a {@link NoSuchAlgorithmException} is caught 169 * @deprecated Use {@link #getSha1Digest()} 170 */ 171 @Deprecated 172 public static MessageDigest getShaDigest() { 173 return getSha1Digest(); 174 } 175 176 /** 177 * Calculates the MD2 digest and returns the value as a 16 element <code>byte[]</code>. 178 * 179 * @param data 180 * Data to digest 181 * @return MD2 digest 182 * @since 1.7 183 */ 184 public static byte[] md2(final byte[] data) { 185 return getMd2Digest().digest(data); 186 } 187 188 /** 189 * Calculates the MD2 digest and returns the value as a 16 element <code>byte[]</code>. 190 * 191 * @param data 192 * Data to digest 193 * @return MD2 digest 194 * @throws IOException 195 * On error reading from the stream 196 * @since 1.7 197 */ 198 public static byte[] md2(final InputStream data) throws IOException { 199 return digest(getMd2Digest(), data); 200 } 201 202 /** 203 * Calculates the MD2 digest and returns the value as a 16 element <code>byte[]</code>. 204 * 205 * @param data 206 * Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)} 207 * @return MD2 digest 208 * @since 1.7 209 */ 210 public static byte[] md2(final String data) { 211 return md2(StringUtils.getBytesUtf8(data)); 212 } 213 214 /** 215 * Calculates the MD2 digest and returns the value as a 32 character hex string. 216 * 217 * @param data 218 * Data to digest 219 * @return MD2 digest as a hex string 220 * @since 1.7 221 */ 222 public static String md2Hex(final byte[] data) { 223 return Hex.encodeHexString(md2(data)); 224 } 225 226 /** 227 * Calculates the MD2 digest and returns the value as a 32 character hex string. 228 * 229 * @param data 230 * Data to digest 231 * @return MD2 digest as a hex string 232 * @throws IOException 233 * On error reading from the stream 234 * @since 1.7 235 */ 236 public static String md2Hex(final InputStream data) throws IOException { 237 return Hex.encodeHexString(md2(data)); 238 } 239 240 /** 241 * Calculates the MD2 digest and returns the value as a 32 character hex string. 242 * 243 * @param data 244 * Data to digest 245 * @return MD2 digest as a hex string 246 * @since 1.7 247 */ 248 public static String md2Hex(final String data) { 249 return Hex.encodeHexString(md2(data)); 250 } 251 252 /** 253 * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>. 254 * 255 * @param data 256 * Data to digest 257 * @return MD5 digest 258 */ 259 public static byte[] md5(final byte[] data) { 260 return getMd5Digest().digest(data); 261 } 262 263 /** 264 * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>. 265 * 266 * @param data 267 * Data to digest 268 * @return MD5 digest 269 * @throws IOException 270 * On error reading from the stream 271 * @since 1.4 272 */ 273 public static byte[] md5(final InputStream data) throws IOException { 274 return digest(getMd5Digest(), data); 275 } 276 277 /** 278 * Calculates the MD5 digest and returns the value as a 16 element <code>byte[]</code>. 279 * 280 * @param data 281 * Data to digest; converted to bytes using {@link StringUtils#getBytesUtf8(String)} 282 * @return MD5 digest 283 */ 284 public static byte[] md5(final String data) { 285 return md5(StringUtils.getBytesUtf8(data)); 286 } 287 288 /** 289 * Calculates the MD5 digest and returns the value as a 32 character hex string. 290 * 291 * @param data 292 * Data to digest 293 * @return MD5 digest as a hex string 294 */ 295 public static String md5Hex(final byte[] data) { 296 return Hex.encodeHexString(md5(data)); 297 } 298 299 /** 300 * Calculates the MD5 digest and returns the value as a 32 character hex string. 301 * 302 * @param data 303 * Data to digest 304 * @return MD5 digest as a hex string 305 * @throws IOException 306 * On error reading from the stream 307 * @since 1.4 308 */ 309 public static String md5Hex(final InputStream data) throws IOException { 310 return Hex.encodeHexString(md5(data)); 311 } 312 313 /** 314 * Calculates the MD5 digest and returns the value as a 32 character hex string. 315 * 316 * @param data 317 * Data to digest 318 * @return MD5 digest as a hex string 319 */ 320 public static String md5Hex(final String data) { 321 return Hex.encodeHexString(md5(data)); 322 } 323 324 /** 325 * Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>. 326 * 327 * @param data 328 * Data to digest 329 * @return SHA-1 digest 330 * @deprecated Use {@link #sha1(byte[])} 331 */ 332 @Deprecated 333 public static byte[] sha(final byte[] data) { 334 return sha1(data); 335 } 336 337 /** 338 * Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>. 339 * 340 * @param data 341 * Data to digest 342 * @return SHA-1 digest 343 * @throws IOException 344 * On error reading from the stream 345 * @since 1.4 346 * @deprecated Use {@link #sha1(InputStream)} 347 */ 348 @Deprecated 349 public static byte[] sha(final InputStream data) throws IOException { 350 return sha1(data); 351 } 352 353 /** 354 * Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>. 355 * 356 * @param data 357 * Data to digest 358 * @return SHA-1 digest 359 * @deprecated Use {@link #sha1(String)} 360 */ 361 @Deprecated 362 public static byte[] sha(final String data) { 363 return sha1(data); 364 } 365 366 /** 367 * Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>. 368 * 369 * @param data 370 * Data to digest 371 * @return SHA-1 digest 372 * @since 1.7 373 */ 374 public static byte[] sha1(final byte[] data) { 375 return getSha1Digest().digest(data); 376 } 377 378 /** 379 * Calculates the SHA-1 digest and returns the value as a <code>byte[]</code>. MD5加密算法Java代码