PadFactory.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /* PadFactory.java --
  2. Copyright (C) 2001, 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
  3. This file is a 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 of the License, or (at
  7. your option) 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; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
  15. 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 gnu.javax.crypto.pad;
  32. import gnu.java.security.Registry;
  33. import java.util.Collections;
  34. import java.util.HashSet;
  35. import java.util.Set;
  36. /**
  37. * A Factory to instantiate padding schemes.
  38. */
  39. public class PadFactory
  40. implements Registry
  41. {
  42. /** Collection of padding algorithm names --cached for speed. */
  43. private static Set names;
  44. /** Trivial constructor to enforce Singleton pattern. */
  45. private PadFactory()
  46. {
  47. super();
  48. }
  49. /**
  50. * Returns an instance of a padding algorithm given its name.
  51. *
  52. * @param pad the case-insensitive name of the padding algorithm.
  53. * @return an instance of the padding algorithm, operating with a given block
  54. * size, or <code>null</code> if none found.
  55. * @throws InternalError if the implementation does not pass its self-test.
  56. */
  57. public static final IPad getInstance(String pad)
  58. {
  59. if (pad == null)
  60. return null;
  61. pad = pad.trim().toLowerCase();
  62. if (pad.endsWith("padding"))
  63. pad = pad.substring(0, pad.length() - "padding".length());
  64. IPad result = null;
  65. if (pad.equals(PKCS7_PAD) || pad.equals(PKCS5_PAD))
  66. result = new PKCS7();
  67. else if (pad.equals(TBC_PAD))
  68. result = new TBC();
  69. else if (pad.equals(EME_PKCS1_V1_5_PAD))
  70. result = new PKCS1_V1_5();
  71. else if (pad.equals(SSL3_PAD))
  72. result = new SSL3();
  73. else if (pad.equals(TLS1_PAD))
  74. result = new TLS1();
  75. else if (pad.equals(ISO10126_PAD))
  76. result = new ISO10126();
  77. if (result != null && ! result.selfTest())
  78. throw new InternalError(result.name());
  79. return result;
  80. }
  81. /**
  82. * Returns a {@link Set} of names of padding algorithms supported by this
  83. * <i>Factory</i>.
  84. *
  85. * @return a {@link Set} of padding algorithm names (Strings).
  86. */
  87. public static final Set getNames()
  88. {
  89. if (names == null)
  90. {
  91. HashSet hs = new HashSet();
  92. hs.add(PKCS5_PAD);
  93. hs.add(PKCS7_PAD);
  94. hs.add(TBC_PAD);
  95. hs.add(EME_PKCS1_V1_5_PAD);
  96. hs.add(SSL3_PAD);
  97. hs.add(TLS1_PAD);
  98. hs.add(ISO10126_PAD);
  99. names = Collections.unmodifiableSet(hs);
  100. }
  101. return names;
  102. }
  103. }