DigestUtils

来源:互联网 发布:普通增值税发票软件 编辑:程序博客网 时间:2024/06/06 05:46
  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 org1.apache.commons.codec.digest;
  17. import java.security.MessageDigest;
  18. import java.security.NoSuchAlgorithmException;
  19. import org1.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 newRuntimeException(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 staticMessageDigest 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 staticMessageDigest 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 staticbyte[] 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 staticbyte[] 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 staticString md5Hex(byte[] data){
  87. return newString(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 staticString md5Hex(String data){
  97. return newString(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 staticbyte[] 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 staticbyte[] 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 staticString shaHex(byte[] data){
  126. return newString(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 staticString shaHex(String data){
  135. return newString(Hex.encodeHex(sha(data)));
  136. }
  137. }
0 0
原创粉丝点击