RC5ParameterSpec.java 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /* RC5ParameterSpec.java -- parameters for RC5.
  2. Copyright (C) 2004 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 javax.crypto.spec;
  32. import java.security.spec.AlgorithmParameterSpec;
  33. /**
  34. * A wrapper for parameters to the <a
  35. * href="http://www.rsasecurity.com/rsalabs/faq/3-6-4.html">RC5</a>
  36. * block cipher.
  37. *
  38. * @author Casey Marshall (csm@gnu.org)
  39. * @since 1.4
  40. */
  41. public class RC5ParameterSpec implements AlgorithmParameterSpec
  42. {
  43. // Fields.
  44. // ------------------------------------------------------------------------
  45. /** The IV. */
  46. private byte[] iv;
  47. /** The number of rounds. */
  48. private int rounds;
  49. /** The version number. */
  50. private int version;
  51. /** The word size, in bits. */
  52. private int wordSize;
  53. // Constructors.
  54. // ------------------------------------------------------------------------
  55. /**
  56. * Create RC5 parameters without an IV.
  57. *
  58. * @param version The version number.
  59. * @param rounds The number of rounds.
  60. * @param wordSize The size of a word, in bits.
  61. */
  62. public RC5ParameterSpec(int version, int rounds, int wordSize)
  63. {
  64. this.version = version;
  65. this.rounds = rounds;
  66. this.wordSize = wordSize;
  67. }
  68. /**
  69. * Create RC5 parameters with an IV. The bytes in <code>iv</code> in
  70. * the range <code>[0, 2*(wordSize/8)-1]</code> are used.
  71. *
  72. * @param version The version number.
  73. * @param rounds The number of rounds.
  74. * @param wordSize The size of a word, in bits.
  75. * @param iv The IV data.
  76. */
  77. public RC5ParameterSpec(int version, int rounds, int wordSize, byte[] iv)
  78. {
  79. this(version, rounds, wordSize, iv, 0);
  80. }
  81. /**
  82. * Create RC5 parameters with an IV. The bytes in <code>iv</code> in
  83. * the range <code>[off, off+2*(wordSize/8)-1]</code> are used.
  84. *
  85. * @param version The version number.
  86. * @param rounds The number of rounds.
  87. * @param wordSize The size of a word, in bits.
  88. * @param iv The IV data.
  89. * @param off From where in the array the IV starts.
  90. */
  91. public
  92. RC5ParameterSpec(int version, int rounds, int wordSize, byte[] iv, int off)
  93. {
  94. this(version, rounds, wordSize);
  95. int ivLength = 2 * (wordSize / 8);
  96. if (off < 0)
  97. throw new IllegalArgumentException();
  98. if (iv.length - off < ivLength)
  99. {
  100. throw new IllegalArgumentException("IV too short");
  101. }
  102. this.iv = new byte[ivLength];
  103. System.arraycopy(iv, off, this.iv, 0, ivLength);
  104. }
  105. // Instance methods.
  106. // ------------------------------------------------------------------------
  107. /**
  108. * Return the initializaiton vector, or <code>null</code> if none was
  109. * specified.
  110. *
  111. * @return The IV, or null.
  112. */
  113. public byte[] getIV()
  114. {
  115. return iv;
  116. }
  117. /**
  118. * Get the number of rounds.
  119. *
  120. * @return The number of rounds.
  121. */
  122. public int getRounds()
  123. {
  124. return rounds;
  125. }
  126. /**
  127. * Get the version number.
  128. *
  129. * @return The version number.
  130. */
  131. public int getVersion()
  132. {
  133. return version;
  134. }
  135. /**
  136. * Get the word size, in bits.
  137. *
  138. * @return The word size, in bits.
  139. */
  140. public int getWordSize()
  141. {
  142. return wordSize;
  143. }
  144. public boolean equals(Object o)
  145. {
  146. if (this == o) return true;
  147. byte[] oiv = ((RC5ParameterSpec) o).getIV();
  148. if (iv != oiv)
  149. {
  150. if (iv == null || oiv == null) return false;
  151. if (iv.length != oiv.length) return false;
  152. for (int i = 0; i < iv.length; i++)
  153. {
  154. if (iv[i] != oiv[i])
  155. {
  156. return false;
  157. }
  158. }
  159. }
  160. return rounds == ((RC5ParameterSpec) o).getRounds()
  161. && version == ((RC5ParameterSpec) o).getVersion()
  162. && wordSize == ((RC5ParameterSpec) o).getWordSize();
  163. }
  164. public int hashCode()
  165. {
  166. int code = rounds + version + wordSize;
  167. if (iv != null)
  168. {
  169. for (int i = 0; i < iv.length; i++)
  170. {
  171. code += iv[i];
  172. }
  173. }
  174. return code;
  175. }
  176. }