PasswordEncryptor.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright (C) 2006-2010 Alfresco Software Limited.
  3. *
  4. * This file is part of Alfresco
  5. *
  6. * Alfresco is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Alfresco is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package org.alfresco.jlan.client;
  20. import java.security.*;
  21. /**
  22. * Password Encryptor Abstract Class
  23. *
  24. * <p>Generates LanMan and NTLMv1 encrypted passwords from the plain text password and challenge key.
  25. *
  26. * @author gkspencer
  27. */
  28. public abstract class PasswordEncryptor {
  29. // Encryption algorithm types
  30. public static final int LANMAN = 0;
  31. public static final int NTLM1 = 1;
  32. public static final int NTLM2 = 2;
  33. public static final int MD4 = 3;
  34. // Encrpytion algorithm names
  35. private final static String[] _algNames = {"LanMan", "NTLMv1", "NTLMv2", "MD4" };
  36. /**
  37. * Default constructor
  38. */
  39. public PasswordEncryptor() {
  40. }
  41. /**
  42. * Check if the required algorithms are available
  43. *
  44. * @return boolean
  45. */
  46. public boolean checkEncryptionAlgorithms() {
  47. return true;
  48. }
  49. /**
  50. * Encrypt the plain text password with the specified encryption key using the specified
  51. * encryption algorithm.
  52. *
  53. * @param plainPwd Plaintext password string
  54. * @param encryptKey byte[] Encryption key
  55. * @param alg int Encryption algorithm
  56. * @return byte[] Encrypted password
  57. * @exception NoSuchAlgorithmException If a required encryption algorithm is not available
  58. */
  59. public abstract byte[] generateEncryptedPassword(String plainPwd, byte[] encryptKey, int alg)
  60. throws NoSuchAlgorithmException;
  61. /**
  62. * Generate a session key using the specified password and key.
  63. *
  64. * @param plainPwd Plaintext password string
  65. * @param encryptKey byte[] Encryption key
  66. * @param alg int Encryption algorithm
  67. * @return byte[] Encrypted password
  68. * @exception NoSuchAlgorithmException If a required encryption algorithm is not available
  69. */
  70. public abstract byte[] generateSessionKey(String plainPwd, byte[] encryptKey, int alg)
  71. throws NoSuchAlgorithmException;
  72. /**
  73. * P16 encryption
  74. *
  75. * @param pwd java.lang.String
  76. * @param s8 byte[]
  77. * @return byte[]
  78. * @exception NoSuchAlgorithmException If a required encryption algorithm is not available
  79. */
  80. public abstract byte[] P16(String pwd, byte[] s8)
  81. throws NoSuchAlgorithmException;
  82. /**
  83. * P24 DES encryption
  84. *
  85. * @param p21 Plain password or hashed password bytes
  86. * @param ch Challenge bytes
  87. * @return Encrypted password
  88. * @exception NoSuchAlgorithmException If a required encryption algorithm is not available
  89. */
  90. protected abstract byte[] P24(byte[] p21, byte[] ch)
  91. throws NoSuchAlgorithmException;
  92. /**
  93. * Return the encryption algorithm as a string
  94. *
  95. * @param alg int
  96. * @return String
  97. */
  98. public static String getAlgorithmName(int alg) {
  99. if ( alg >= 0 && alg < _algNames.length)
  100. return _algNames[alg];
  101. return "Unknown";
  102. }
  103. }