Coverage Summary for Class: PasswordEncoderDecoder (com.acciente.oacc.encryptor.bcrypt)

Class Class, % Method, % Line, %
PasswordEncoderDecoder 100% (1/ 1) 100% (3/ 3) 100% (5/ 5)


1 /* 2  * Copyright 2009-2018, Acciente LLC 3  * 4  * Acciente LLC licenses this file to you under the 5  * Apache License, Version 2.0 (the "License"); you 6  * may not use this file except in compliance with the 7  * License. You may obtain a copy of the License at 8  * 9  * http://www.apache.org/licenses/LICENSE-2.0 10  * 11  * Unless required by applicable law or agreed to in 12  * writing, software distributed under the License is 13  * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES 14  * OR CONDITIONS OF ANY KIND, either express or implied. 15  * See the License for the specific language governing 16  * permissions and limitations under the License. 17  */ 18  19 package com.acciente.oacc.encryptor.bcrypt; 20  21 import org.bouncycastle.crypto.generators.OpenBSDBCrypt; 22  23 public class PasswordEncoderDecoder { 24  private static final String MARKER = BCryptPasswordEncryptor.NAME + ":"; 25  26  /** 27  * Encodes the OACC password header into the BCrypt string (OpenBSD standard BCrypt hash implementation). 28  * 29  * @param bcryptString the BCrypt string returned by an OpenBSD standard BCrypt hashing implementation that "includes 30  * version, cost factor, salt and hash, separated by '$'" (from the Javadoc for 31  * {@link OpenBSDBCrypt#generate(char[], byte[], int)}. 32  * @return a fully-encoded password ready for persistent storage. 33  */ 34  String encode(String bcryptString) { 35  return MARKER + bcryptString; 36  37  } 38  39  /** 40  * Decodes the encoded BCrypt string (OpenBSD standard BCrypt hash implementation) from an encoded password. 41  * 42  * @param encodedPassword an encoded password that was previously returned by {{@link #encode(String)}}. 43  * @return a BCrypt string (OpenBSD standard BCrypt hashing implementation). 44  */ 45  String decode(String encodedPassword) { 46  if (encodedPassword.startsWith(MARKER)) { 47  return encodedPassword.substring(MARKER.length()); 48  } 49  else { 50  throw new IllegalArgumentException("Unexpected marker for BCrypt password: " + 51  encodedPassword.substring(0, MARKER.length())); 52  } 53  } 54 }