PageRenderTime 49ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/gnu-crypto/source/gnu/crypto/cipher/CipherFactory.java

https://github.com/clibrepo/04f6ea02286af71632b72d73554979a61da424cf681886674b21ecd595715c12
Java | 150 lines | 63 code | 15 blank | 72 comment | 30 complexity | e09834159dfbb89961cd2ec359ef0e1d MD5 | raw file
  1. package gnu.crypto.cipher;
  2. // ----------------------------------------------------------------------------
  3. // $Id: CipherFactory.java,v 1.11 2003/06/14 14:43:18 raif Exp $
  4. //
  5. // Copyright (C) 2001, 2002, 2003 Free Software Foundation, Inc.
  6. //
  7. // This file is part of GNU Crypto.
  8. //
  9. // GNU Crypto is free software; you can redistribute it and/or modify
  10. // it under the terms of the GNU General Public License as published by
  11. // the Free Software Foundation; either version 2, or (at your option)
  12. // any later version.
  13. //
  14. // GNU Crypto is distributed in the hope that it will be useful, but
  15. // WITHOUT ANY WARRANTY; without even the implied warranty of
  16. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. // General Public License for more details.
  18. //
  19. // You should have received a copy of the GNU General Public License
  20. // along with this program; see the file COPYING. If not, write to the
  21. //
  22. // Free Software Foundation Inc.,
  23. // 59 Temple Place - Suite 330,
  24. // Boston, MA 02111-1307
  25. // USA
  26. //
  27. // Linking this library statically or dynamically with other modules is
  28. // making a combined work based on this library. Thus, the terms and
  29. // conditions of the GNU General Public License cover the whole
  30. // combination.
  31. //
  32. // As a special exception, the copyright holders of this library give
  33. // you permission to link this library with independent modules to
  34. // produce an executable, regardless of the license terms of these
  35. // independent modules, and to copy and distribute the resulting
  36. // executable under terms of your choice, provided that you also meet,
  37. // for each linked independent module, the terms and conditions of the
  38. // license of that module. An independent module is a module which is
  39. // not derived from or based on this library. If you modify this
  40. // library, you may extend this exception to your version of the
  41. // library, but you are not obligated to do so. If you do not wish to
  42. // do so, delete this exception statement from your version.
  43. // ----------------------------------------------------------------------------
  44. import gnu.crypto.Registry;
  45. import java.util.Collections;
  46. import java.util.HashSet;
  47. import java.util.Set;
  48. /**
  49. * <p>A <i>Factory</i> to instantiate symmetric block cipher instances.</p>
  50. *
  51. * @version $Revision: 1.11 $
  52. */
  53. public class CipherFactory implements Registry {
  54. // Constants and variables
  55. // -------------------------------------------------------------------------
  56. // Constructor(s)
  57. // -------------------------------------------------------------------------
  58. /** Trivial constructor to enforce Singleton pattern. */
  59. private CipherFactory() {
  60. super();
  61. }
  62. // Class methods
  63. // -------------------------------------------------------------------------
  64. /**
  65. * <p>Returns an instance of a block cipher given its name.</p>
  66. *
  67. * @param name the case-insensitive name of the symmetric-key block cipher
  68. * algorithm.
  69. * @return an instance of the designated cipher algorithm, or
  70. * <code>null</code> if none is found.
  71. * @exception InternalError if the implementation does not pass its
  72. * self-test.
  73. */
  74. public static final IBlockCipher getInstance(String name) {
  75. if (name == null) {
  76. return null;
  77. }
  78. name = name.trim();
  79. IBlockCipher result = null;
  80. if (name.equalsIgnoreCase(ANUBIS_CIPHER)) {
  81. result = new Anubis();
  82. } else if (name.equalsIgnoreCase(BLOWFISH_CIPHER)) {
  83. result = new Blowfish();
  84. } else if (name.equalsIgnoreCase(DES_CIPHER)) {
  85. result = new DES();
  86. } else if (name.equalsIgnoreCase(KHAZAD_CIPHER)) {
  87. result = new Khazad();
  88. } else if (name.equalsIgnoreCase(RIJNDAEL_CIPHER)
  89. || name.equalsIgnoreCase(AES_CIPHER)) {
  90. result = new Rijndael();
  91. } else if (name.equalsIgnoreCase(SERPENT_CIPHER)) {
  92. result = new Serpent();
  93. } else if (name.equalsIgnoreCase(SQUARE_CIPHER)) {
  94. result = new Square();
  95. } else if (name.equalsIgnoreCase(TRIPLEDES_CIPHER)
  96. || name.equalsIgnoreCase(DESEDE_CIPHER)) {
  97. result = new TripleDES();
  98. } else if (name.equalsIgnoreCase(TWOFISH_CIPHER)) {
  99. result = new Twofish();
  100. } else if (name.equalsIgnoreCase(CAST5_CIPHER)
  101. || (name.equalsIgnoreCase(CAST128_CIPHER)
  102. || (name.equalsIgnoreCase(CAST_128_CIPHER)))) {
  103. result = new Cast5();
  104. } else if (name.equalsIgnoreCase(NULL_CIPHER)) {
  105. result = new NullCipher();
  106. }
  107. if (result != null && !result.selfTest()) {
  108. throw new InternalError(result.name());
  109. }
  110. return result;
  111. }
  112. /**
  113. * <p>Returns a {@link Set} of symmetric key block cipher implementation
  114. * names supported by this <i>Factory</i>.</p>
  115. *
  116. * @return a {@link Set} of block cipher names (Strings).
  117. */
  118. public static final Set getNames() {
  119. HashSet hs = new HashSet();
  120. hs.add(ANUBIS_CIPHER);
  121. hs.add(BLOWFISH_CIPHER);
  122. hs.add(DES_CIPHER);
  123. hs.add(KHAZAD_CIPHER);
  124. hs.add(RIJNDAEL_CIPHER);
  125. hs.add(SERPENT_CIPHER);
  126. hs.add(SQUARE_CIPHER);
  127. hs.add(TRIPLEDES_CIPHER);
  128. hs.add(TWOFISH_CIPHER);
  129. hs.add(CAST5_CIPHER);
  130. hs.add(NULL_CIPHER);
  131. return Collections.unmodifiableSet(hs);
  132. }
  133. // Instance methods
  134. // -------------------------------------------------------------------------
  135. }