protected String getString(GuardedString guardedString) { if (guardedString == null) { return null; } final String[] passwdArray = { null }; guardedString.access(new Accessor() { @Override public void access(char[] passwdChars) { String password = new String(passwdChars); checkPasswordPolicies(password); passwdArray[0] = password; } }); return passwdArray[0]; }
@Override public void serialize(final Object object, final ObjectEncoder encoder) { final GuardedString val = (GuardedString) object; val.access(new GuardedString.Accessor() { @Override public void access(final char[] clearChars) { byte[] encryptedBytes = null; byte[] clearBytes = null; try { clearBytes = SecurityUtil.charsToBytes(clearChars); encryptedBytes = EncryptorFactory.getInstance().getDefaultEncryptor().encrypt(clearBytes); encoder.writeByteArrayContents(encryptedBytes); } finally { SecurityUtil.clear(encryptedBytes); SecurityUtil.clear(clearBytes); } } }); } });
/** * Decrypts the value of a {@link GuardedString}. * * @param guardedString the guarded string value. * @return the clear string value. * @since 1.4 */ public static String decrypt(GuardedString guardedString) { String[] clearText = new String[1]; guardedString.access((clearChars) -> { clearText[0] = new String(clearChars); }); return clearText[0]; }
/** * Decrypts the value of a {@link GuardedString}. * * @param guardedString the guarded string value. * @return the clear string value. * @since 1.4 */ public static String decrypt(GuardedString guardedString) { String[] clearText = new String[1]; guardedString.access((clearChars) -> { clearText[0] = new String(clearChars); }); return clearText[0]; }
public String toSecureString() { if (value == null) { return ""; } else { final List<String> res = new ArrayList<String>(); ((GuardedString) value.get(0)).access(new GuardedString.Accessor() { @Override public void access(final char[] clearChars) { res.add(new String(clearChars)); } }); return res.get(0); } }
throws SQLException { try { guard.access(new GuardedString.Accessor() { public void access(char[] clearChars) { try {
private ProtectedStringType fromGuardedString(GuardedString icfValue) { final ProtectedStringType ps = new ProtectedStringType(); icfValue.access(new GuardedString.Accessor() { @Override public void access(char[] passwordChars) { try { ps.setClearValue(new String(passwordChars)); protector.encrypt(ps); } catch (EncryptionException e) { throw new IllegalStateException("Protector failed to encrypt password"); } } }); return ps; } }
new CSVDirFilterTranslator(this, objectClass, options); password.access(new GuardedString.Accessor() {
/** * Highly insecure method! Do not do this in production code. This is only * for test purposes */ private String decryptToString(GuardedString string) { final StringBuilder buf = new StringBuilder(); string.access((char[] chars) -> { buf.append(chars); }); return buf.toString(); } }
password.access(new GuardedString.Accessor() { public void access(char[] clearChars) { try {
final DataSource ds = (DataSource) ic.lookup(datasourceName); final Connection[] ret = new Connection[1]; password.access(new GuardedString.Accessor() { public void access(char[] clearChars) { try {
guardedString.access(new GuardedString.Accessor() { @Override public void access(char[] decryptedChars) {
@Test public void testUnicode() { for (int i = 0; i < 0xFFFF; i++) { final int expected = i; char c = (char) i; GuardedString gs = new GuardedString(new char[] { c }); gs.access((char[] clearChars) -> { int v = (int) clearChars[0]; assertEquals(v, expected); }); } }
protected void applyModifyMetadata(DummyObject object, OperationOptions options) throws ConnectException, FileNotFoundException, SchemaViolationException, ConflictException, InterruptedException { String runAsUser = options.getRunAsUser(); if (runAsUser != null) { if (!configuration.getSupportRunAs()) { throw new UnsupportedOperationException("runAsUser option is not supported"); } DummyAccount runAsAccount = resource.getAccountByUsername(runAsUser); if (runAsAccount == null) { new ConfigurationException("No runAsUser "+runAsUser); } GuardedString runWithPassword = options.getRunWithPassword(); if (runWithPassword != null) { runWithPassword.access((clearChars) -> { if (!runAsAccount.getPassword().equals(new String(clearChars))) { throw new InvalidPasswordException("Wrong runWithPassword"); } }); } else { throw new InvalidPasswordException("No runWithPassword"); } object.setLastModifier(runAsAccount.getName()); } else { object.setLastModifier(null); } }
@Override public void serialize(final Object object, final ObjectEncoder encoder) { final GuardedString val = (GuardedString) object; val.access((final char[] clearChars) -> { byte[] encryptedBytes = null; byte[] clearBytes = null; try { clearBytes = SecurityUtil.charsToBytes(clearChars); encryptedBytes = EncryptorFactory.getInstance().getDefaultEncryptor().encrypt(clearBytes); encoder.writeByteArrayContents(encryptedBytes); } finally { SecurityUtil.clear(encryptedBytes); SecurityUtil.clear(clearBytes); } }); }
@Override public void serialize(final Object object, final ObjectEncoder encoder) { final GuardedString val = (GuardedString) object; val.access((final char[] clearChars) -> { byte[] encryptedBytes = null; byte[] clearBytes = null; try { clearBytes = SecurityUtil.charsToBytes(clearChars); encryptedBytes = EncryptorFactory.getInstance().getDefaultEncryptor().encrypt(clearBytes); encoder.writeByteArrayContents(encryptedBytes); } finally { SecurityUtil.clear(encryptedBytes); SecurityUtil.clear(clearBytes); } }); }
resource.setUselessGuardedString(null); } else { uselessGuardedString.access(chars -> resource.setUselessGuardedString(new String(chars)));