| | |
| | | public ByteString encodePassword(ByteSequence plaintext) |
| | | throws DirectoryException |
| | | { |
| | | byte[] saltBytes = new byte[NUM_SALT_BYTES]; |
| | | byte[] saltBytes = new byte[NUM_SALT_BYTES]; |
| | | byte[] digestBytes; |
| | | int iterations = config.getPBKDF2Iterations(); |
| | | char[] plaintextChars = null; |
| | | int iterations = config.getPBKDF2Iterations(); |
| | | |
| | | synchronized(factoryLock) |
| | | { |
| | |
| | | { |
| | | random.nextBytes(saltBytes); |
| | | |
| | | KeySpec spec = new PBEKeySpec(plaintext.toString().toCharArray(), |
| | | plaintextChars = plaintext.toString().toCharArray(); |
| | | KeySpec spec = new PBEKeySpec(plaintextChars, |
| | | saltBytes, iterations, SHA1_LENGTH * 8); |
| | | digestBytes = factory.generateSecret(spec).getEncoded(); |
| | | } |
| | |
| | | throw new DirectoryException(DirectoryServer.getServerErrorResultCode(), |
| | | message, e); |
| | | } |
| | | finally |
| | | { |
| | | if (plaintextChars != null) |
| | | Arrays.fill(plaintextChars, '0'); |
| | | } |
| | | } |
| | | // Append the salt to the hashed value and base64-the whole thing. |
| | | byte[] hashPlusSalt = new byte[digestBytes.length + NUM_SALT_BYTES]; |
| | |
| | | saltLength); |
| | | |
| | | byte[] userDigestBytes; |
| | | char[] plaintextChars = null; |
| | | |
| | | synchronized (factoryLock) |
| | | { |
| | | try |
| | | { |
| | | plaintextChars = plaintextPassword.toString().toCharArray(); |
| | | KeySpec spec = new PBEKeySpec( |
| | | plaintextPassword.toString().toCharArray(), saltBytes, |
| | | plaintextChars, saltBytes, |
| | | iterations, SHA1_LENGTH * 8); |
| | | userDigestBytes = factory.generateSecret(spec).getEncoded(); |
| | | } |
| | |
| | | |
| | | return false; |
| | | } |
| | | finally |
| | | { |
| | | if (plaintextChars != null) |
| | | Arrays.fill(plaintextChars, '0'); |
| | | } |
| | | } |
| | | |
| | | return Arrays.equals(digestBytes, userDigestBytes); |
| | |
| | | public ByteString encodeAuthPassword(ByteSequence plaintext) |
| | | throws DirectoryException |
| | | { |
| | | byte[] saltBytes = new byte[NUM_SALT_BYTES]; |
| | | byte[] saltBytes = new byte[NUM_SALT_BYTES]; |
| | | byte[] digestBytes; |
| | | int iterations = config.getPBKDF2Iterations(); |
| | | char[] plaintextChars = null; |
| | | int iterations = config.getPBKDF2Iterations(); |
| | | |
| | | synchronized(factoryLock) |
| | | { |
| | |
| | | { |
| | | random.nextBytes(saltBytes); |
| | | |
| | | plaintextChars = plaintext.toString().toCharArray(); |
| | | KeySpec spec = new PBEKeySpec( |
| | | plaintext.toString().toCharArray(), saltBytes, |
| | | plaintextChars, saltBytes, |
| | | iterations, SHA1_LENGTH * 8); |
| | | digestBytes = factory.generateSecret(spec).getEncoded(); |
| | | } |
| | |
| | | throw new DirectoryException(DirectoryServer.getServerErrorResultCode(), |
| | | message, e); |
| | | } |
| | | finally |
| | | { |
| | | if (plaintextChars != null) |
| | | Arrays.fill(plaintextChars, '0'); |
| | | } |
| | | } |
| | | // Encode and return the value. |
| | | StringBuilder authPWValue = new StringBuilder(); |
| | |
| | | saltBytes.length); |
| | | |
| | | byte[] userDigestBytes; |
| | | char[] plaintextChars = null; |
| | | |
| | | synchronized (factoryLock) |
| | | { |
| | | try |
| | | { |
| | | plaintextChars = plaintextPassword.toString().toCharArray(); |
| | | KeySpec spec = new PBEKeySpec( |
| | | plaintextPassword.toString().toCharArray(), saltBytes, |
| | | plaintextChars, saltBytes, |
| | | iterations, SHA1_LENGTH * 8); |
| | | userDigestBytes = factory.generateSecret(spec).getEncoded(); |
| | | } |
| | |
| | | |
| | | return false; |
| | | } |
| | | finally |
| | | { |
| | | if (plaintextChars != null) |
| | | Arrays.fill(plaintextChars, '0'); |
| | | } |
| | | } |
| | | |
| | | return Arrays.equals(digestBytes, userDigestBytes); |
| | |
| | | public static String encodeOffline(byte[] passwordBytes) |
| | | throws DirectoryException |
| | | { |
| | | byte[] saltBytes = new byte[NUM_SALT_BYTES]; |
| | | byte[] saltBytes = new byte[NUM_SALT_BYTES]; |
| | | byte[] digestBytes; |
| | | int iterations = 10000; |
| | | char[] plaintextChars = null; |
| | | int iterations = 10000; |
| | | |
| | | try |
| | | { |
| | | SecureRandom.getInstance(SECURE_PRNG_SHA1).nextBytes(saltBytes); |
| | | |
| | | plaintextChars = passwordBytes.toString().toCharArray(); |
| | | KeySpec spec = new PBEKeySpec( |
| | | passwordBytes.toString().toCharArray(), saltBytes, |
| | | plaintextChars, saltBytes, |
| | | iterations, SHA1_LENGTH * 8); |
| | | digestBytes = SecretKeyFactory |
| | | .getInstance(MESSAGE_DIGEST_ALGORITHM_PBKDF2) |
| | |
| | | throw new DirectoryException(DirectoryServer.getServerErrorResultCode(), |
| | | message, e); |
| | | } |
| | | finally |
| | | { |
| | | if (plaintextChars != null) |
| | | Arrays.fill(plaintextChars, '0'); |
| | | } |
| | | |
| | | // Append the salt to the hashed value and base64-the whole thing. |
| | | byte[] hashPlusSalt = new byte[digestBytes.length + NUM_SALT_BYTES]; |