GrapesAuthenticator implements Authenticator<BasicCredentials, DbCredential> { public static String encrypt(final String password) throws AuthenticationException { String hashValue; try { final MessageDigest msgDigest = MessageDigest.getInstance("SHA"); msgDigest.update(password.getBytes("UTF-8")); final byte rawByte[] = msgDigest.digest(); hashValue = new String(Base64.encodeBase64(rawByte)); } catch (Exception e) { LOG.error("Encryption failed."); throw new AuthenticationException("Error occurred during password encryption", e); } return hashValue; } GrapesAuthenticator(final RepositoryHandler repoHandler); @Override Optional<DbCredential> authenticate(BasicCredentials credentials); static String encrypt(final String password);  }
@Test public void testEncryption() { final String password = "myPassword"; Exception exception = null; String encryptedPassword1 = null, encryptedPassword2 = null; try { encryptedPassword1 = GrapesAuthenticator.encrypt(password); encryptedPassword2 = GrapesAuthenticator.encrypt(password); } catch (Exception e) { exception = e; } assertNull(exception); assertNotNull(encryptedPassword1); assertFalse(password.equals(encryptedPassword1)); assertNotNull(encryptedPassword2); assertEquals(encryptedPassword1, encryptedPassword2); }
@Test public void testWrongEncryption() { final String password = null; Exception exception = null; try { GrapesAuthenticator.encrypt(password); } catch (Exception e) { exception = e; } assertNotNull(exception); }