1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.commons.codec.digest;
  17. import java.security.MessageDigest;
  18. import java.security.NoSuchAlgorithmException;
  19. import org.apache.commons.codec.binary.Hex;
  20. /**
  21. * Operations to simplifiy common {@link java.security.MessageDigest} tasks. This
  22. * class is thread safe.
  23. *
  24. * @author Apache Software Foundation
  25. */
  26. public class DigestUtils {
  27. /**
  28. * Returns a MessageDigest for the given <code>algorithm</code>.
  29. *
  30. * @param algorithm The MessageDigest algorithm name.
  31. * @return An MD5 digest instance.
  32. * @throws RuntimeException when a {@link java.security.NoSuchAlgorithmException} is caught,
  33. */
  34. static MessageDigest getDigest(String algorithm) {
  35. try {
  36. return MessageDigest.getInstance(algorithm);
  37. } catch (NoSuchAlgorithmException e) {
  38. throw new RuntimeException(e.getMessage());
  39. }
  40. }
  41. /**
  42. * Returns an MD5 MessageDigest.
  43. *
  44. * @return An MD5 digest instance.
  45. * @throws RuntimeException when a {@link java.security.NoSuchAlgorithmException} is caught,
  46. */
  47. private static MessageDigest getMd5Digest() {
  48. return getDigest("MD5");
  49. }
  50. /**
  51. * Returns an SHA digest.
  52. *
  53. * @return An SHA digest instance.
  54. * @throws RuntimeException when a {@link java.security.NoSuchAlgorithmException} is caught,
  55. */
  56. private static MessageDigest getShaDigest() {
  57. return getDigest("SHA");
  58. }
  59. /**
  60. * Calculates the MD5 digest and returns the value as a 16 element
  61. * <code>byte[]</code>.
  62. *
  63. * @param data Data to digest
  64. * @return MD5 digest
  65. */
  66. public static byte[] md5(byte[] data) {
  67. return getMd5Digest().digest(data);
  68. }
  69. /**
  70. * Calculates the MD5 digest and returns the value as a 16 element
  71. * <code>byte[]</code>.
  72. *
  73. * @param data Data to digest
  74. * @return MD5 digest
  75. */
  76. public static byte[] md5(String data) {
  77. return md5(data.getBytes());
  78. }
  79. /**
  80. * Calculates the MD5 digest and returns the value as a 32 character
  81. * hex string.
  82. *
  83. * @param data Data to digest
  84. * @return MD5 digest as a hex string
  85. */
  86. public static String md5Hex(byte[] data) {
  87. return new String(Hex.encodeHex(md5(data)));
  88. }
  89. /**
  90. * Calculates the MD5 digest and returns the value as a 32 character
  91. * hex string.
  92. *
  93. * @param data Data to digest
  94. * @return MD5 digest as a hex string
  95. */
  96. public static String md5Hex(String data) {
  97. return new String(Hex.encodeHex(md5(data)));
  98. }
  99. /**
  100. * Calculates the SHA digest and returns the value as a
  101. * <code>byte[]</code>.
  102. *
  103. * @param data Data to digest
  104. * @return SHA digest
  105. */
  106. public static byte[] sha(byte[] data) {
  107. return getShaDigest().digest(data);
  108. }
  109. /**
  110. * Calculates the SHA digest and returns the value as a
  111. * <code>byte[]</code>.
  112. *
  113. * @param data Data to digest
  114. * @return SHA digest
  115. */
  116. public static byte[] sha(String data) {
  117. return sha(data.getBytes());
  118. }
  119. /**
  120. * Calculates the SHA digest and returns the value as a hex string.
  121. *
  122. * @param data Data to digest
  123. * @return SHA digest as a hex string
  124. */
  125. public static String shaHex(byte[] data) {
  126. return new String(Hex.encodeHex(sha(data)));
  127. }
  128. /**
  129. * Calculates the SHA digest and returns the value as a hex string.
  130. *
  131. * @param data Data to digest
  132. * @return SHA digest as a hex string
  133. */
  134. public static String shaHex(String data) {
  135. return new String(Hex.encodeHex(sha(data)));
  136. }
  137. }