MessageDigestSpi.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /* MessageDigestSpi.java --- The message digest service provider interface.
  2. Copyright (C) 1999, 2005 Free Software Foundation, Inc.
  3. This file is part of GNU Classpath.
  4. GNU Classpath is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8. GNU Classpath is distributed in the hope that it will be useful, but
  9. WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GNU Classpath; see the file COPYING. If not, write to the
  14. Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  15. 02110-1301 USA.
  16. Linking this library statically or dynamically with other modules is
  17. making a combined work based on this library. Thus, the terms and
  18. conditions of the GNU General Public License cover the whole
  19. combination.
  20. As a special exception, the copyright holders of this library give you
  21. permission to link this library with independent modules to produce an
  22. executable, regardless of the license terms of these independent
  23. modules, and to copy and distribute the resulting executable under
  24. terms of your choice, provided that you also meet, for each linked
  25. independent module, the terms and conditions of the license of that
  26. module. An independent module is a module which is not derived from
  27. or based on this library. If you modify this library, you may extend
  28. this exception to your version of the library, but you are not
  29. obligated to do so. If you do not wish to do so, delete this
  30. exception statement from your version. */
  31. package java.security;
  32. import java.nio.ByteBuffer;
  33. /**
  34. This is the Service Provider Interface (SPI) for MessageDigest
  35. class in java.security. It provides the back end functionality
  36. for the MessageDigest class so that it can compute message
  37. hashes. The default hashes are SHA-1 and MD5. A message hash
  38. takes data of arbitrary length and produces a unique number
  39. representing it.
  40. Cryptography service providers who want to implement their
  41. own message digest hashes need only to subclass this class.
  42. The implementation of a Cloneable interface is left to up to
  43. the programmer of a subclass.
  44. @version 0.0
  45. @author Mark Benvenuto (ivymccough@worldnet.att.net)
  46. */
  47. public abstract class MessageDigestSpi
  48. {
  49. /**
  50. Default constructor of the MessageDigestSpi class
  51. */
  52. public MessageDigestSpi()
  53. {
  54. }
  55. /**
  56. Returns the length of the digest. It may be overridden by the
  57. provider to return the length of the digest. Default is to
  58. return 0. It is concrete for backwards compatibility with JDK1.1
  59. message digest classes.
  60. @return Length of Digest in Bytes
  61. @since 1.2
  62. */
  63. protected int engineGetDigestLength()
  64. {
  65. return 0;
  66. }
  67. /**
  68. Updates the digest with the specified byte.
  69. @param input the byte to update digest with
  70. */
  71. protected abstract void engineUpdate(byte input);
  72. /**
  73. Updates the digest with the specified bytes starting with the
  74. offset and proceeding for the specified length.
  75. @param input the byte array to update digest with
  76. @param offset the offset of the byte to start with
  77. @param len the number of the bytes to update with
  78. */
  79. protected abstract void engineUpdate(byte[]input, int offset, int len);
  80. /**
  81. * Updates this digest with the remaining bytes of a byte buffer.
  82. *
  83. * @param input The input buffer.
  84. * @since 1.5
  85. */
  86. protected void engineUpdate (ByteBuffer input)
  87. {
  88. byte[] buf = new byte[1024];
  89. while (input.hasRemaining())
  90. {
  91. int n = Math.min(input.remaining(), buf.length);
  92. input.get (buf, 0, n);
  93. engineUpdate (buf, 0, n);
  94. }
  95. }
  96. /**
  97. Computes the final digest of the stored bytes and returns
  98. them. It performs any necessary padding. The message digest
  99. should reset sensitive data after performing the digest.
  100. @return An array of bytes containing the digest
  101. */
  102. protected abstract byte[] engineDigest();
  103. /**
  104. Computes the final digest of the stored bytes and returns
  105. them. It performs any necessary padding. The message digest
  106. should reset sensitive data after performing the digest. This
  107. method is left concrete for backwards compatibility with JDK1.1
  108. message digest classes.
  109. @param buf An array of bytes to store the digest
  110. @param offset An offset to start storing the digest at
  111. @param len The length of the buffer
  112. @return Returns the length of the buffer
  113. @since 1.2
  114. */
  115. protected int engineDigest(byte[]buf, int offset, int len)
  116. throws DigestException
  117. {
  118. if (engineGetDigestLength() > len)
  119. throw new DigestException("Buffer is too small.");
  120. byte[] tmp = engineDigest();
  121. if (tmp.length > len)
  122. throw new DigestException("Buffer is too small");
  123. System.arraycopy(tmp, 0, buf, offset, tmp.length);
  124. return tmp.length;
  125. }
  126. /**
  127. Resets the digest engine. Reinitializes internal variables
  128. and clears sensitive data.
  129. */
  130. protected abstract void engineReset();
  131. /**
  132. Returns a clone of this class.
  133. If cloning is not supported, then by default the class throws a
  134. CloneNotSupportedException. The MessageDigestSpi provider
  135. implementation has to overload this class in order to be
  136. cloneable.
  137. */
  138. public Object clone() throws CloneNotSupportedException
  139. {
  140. return super.clone();
  141. }
  142. }