PageRenderTime 33ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/gnu/javax/crypto/cipher/CipherFactory.java

https://github.com/penberg/classpath
Java | 129 lines | 65 code | 9 blank | 55 comment | 30 complexity | ddd26c22e1562fab8430acb7556eac16 MD5 | raw file
  1. /* CipherFactory.java --
  2. Copyright (C) 2001, 2002, 2003, 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.cipher;
  32. import gnu.java.security.Registry;
  33. import java.util.Collections;
  34. import java.util.HashSet;
  35. import java.util.Set;
  36. /**
  37. * A <i>Factory</i> to instantiate symmetric block cipher instances.
  38. */
  39. public class CipherFactory
  40. implements Registry
  41. {
  42. /** Trivial constructor to enforce Singleton pattern. */
  43. private CipherFactory()
  44. {
  45. super();
  46. }
  47. /**
  48. * Returns an instance of a block cipher given its name.
  49. *
  50. * @param name the case-insensitive name of the symmetric-key block cipher
  51. * algorithm.
  52. * @return an instance of the designated cipher algorithm, or
  53. * <code>null</code> if none is found.
  54. * @exception InternalError if the implementation does not pass its self-test.
  55. */
  56. public static final IBlockCipher getInstance(String name)
  57. {
  58. if (name == null)
  59. return null;
  60. name = name.trim();
  61. IBlockCipher result = null;
  62. if (name.equalsIgnoreCase(ANUBIS_CIPHER))
  63. result = new Anubis();
  64. else if (name.equalsIgnoreCase(BLOWFISH_CIPHER))
  65. result = new Blowfish();
  66. else if (name.equalsIgnoreCase(DES_CIPHER))
  67. result = new DES();
  68. else if (name.equalsIgnoreCase(KHAZAD_CIPHER))
  69. result = new Khazad();
  70. else if (name.equalsIgnoreCase(RIJNDAEL_CIPHER)
  71. || name.equalsIgnoreCase(AES_CIPHER))
  72. result = new Rijndael();
  73. else if (name.equalsIgnoreCase(SERPENT_CIPHER))
  74. result = new Serpent();
  75. else if (name.equalsIgnoreCase(SQUARE_CIPHER))
  76. result = new Square();
  77. else if (name.equalsIgnoreCase(TRIPLEDES_CIPHER)
  78. || name.equalsIgnoreCase(DESEDE_CIPHER))
  79. result = new TripleDES();
  80. else if (name.equalsIgnoreCase(TWOFISH_CIPHER))
  81. result = new Twofish();
  82. else if (name.equalsIgnoreCase(CAST5_CIPHER)
  83. || (name.equalsIgnoreCase(CAST128_CIPHER)
  84. || (name.equalsIgnoreCase(CAST_128_CIPHER))))
  85. result = new Cast5();
  86. else if (name.equalsIgnoreCase(NULL_CIPHER))
  87. result = new NullCipher();
  88. if (result != null && ! result.selfTest())
  89. throw new InternalError(result.name());
  90. return result;
  91. }
  92. /**
  93. * Returns a {@link Set} of symmetric key block cipher implementation names
  94. * supported by this <i>Factory</i>.
  95. *
  96. * @return a {@link Set} of block cipher names (Strings).
  97. */
  98. public static final Set getNames()
  99. {
  100. HashSet hs = new HashSet();
  101. hs.add(ANUBIS_CIPHER);
  102. hs.add(BLOWFISH_CIPHER);
  103. hs.add(DES_CIPHER);
  104. hs.add(KHAZAD_CIPHER);
  105. hs.add(RIJNDAEL_CIPHER);
  106. hs.add(SERPENT_CIPHER);
  107. hs.add(SQUARE_CIPHER);
  108. hs.add(TRIPLEDES_CIPHER);
  109. hs.add(TWOFISH_CIPHER);
  110. hs.add(CAST5_CIPHER);
  111. hs.add(NULL_CIPHER);
  112. return Collections.unmodifiableSet(hs);
  113. }
  114. }