public static byte[] decodeBase64Content(byte[] content) { return decodeBase64Content(content, false); }
public static String encodeBase64Content(byte[] value) { return encodeBase64Content(value, false); }
static String base64RandomUuid () { return Helpers.base64FromUuid(UUID.randomUUID()); }
byte[] bytes; if (base64in) { bytes = Helpers.decodeBase64Content(input.getBytes(), false); } else { bytes = input.getBytes(); return Helpers.encodeBase64Content(output, false);
public XMLEvent transform (XMLEvent event) { switch (event.getEventType()) { case START_ELEMENT: { Attribute attribute = event.asStartElement().getAttributeByName(new QName("Protected")); if (attribute != null) { decryptContent = Helpers.toBoolean(attribute.getValue()); } break; } case END_ELEMENT: { decryptContent = false; break; } case CHARACTERS: { if (decryptContent) { String text = event.asCharacters().getData(); text = new String(streamEncryptor.decrypt(Helpers.decodeBase64Content(text.getBytes(), false))); event = xmlEventFactory.createCharacters(text); } break; } } return event; } }
Attribute attribute = event.asStartElement().getAttributeByName(new QName("Protected")); if (attribute != null) { encryptContent = Helpers.toBoolean(attribute.getValue()); if (encryptContent) { String unencrypted = event.asCharacters().getData(); String encrypted = Helpers.encodeBase64Content(encryptor.encrypt(unencrypted.getBytes()), false); event = eventFactory.createCharacters(encrypted);
@Override public DomGroupWrapper getRecycleBin() { String UUIDcontent = getElementContent(RECYCLE_BIN_UUID_ELEMENT_NAME, dbMeta); if (UUIDcontent != null){ final UUID uuid = Helpers.uuidFromBase64(UUIDcontent); if (uuid.getLeastSignificantBits() != 0 && uuid.getMostSignificantBits() != 0) { for (DomGroupWrapper g: getRootGroup().getGroups()) { if (g.getUuid().equals(uuid)) { return g; } } // the recycle bin seems to have been lost, better create another one } // uuid was 0 i.e. there isn't one } // no recycle bin group set up if (!isRecycleBinEnabled()) { return null; } DomGroupWrapper g = newGroup(); g.setName("Recycle Bin"); getRootGroup().addGroup(g); ensureElementContent(RECYCLE_BIN_UUID_ELEMENT_NAME, dbMeta, base64FromUuid(g.getUuid())); touchElement(RECYCLE_BIN_CHANGED_ELEMENT_NAME, dbMeta); return g; }
@Override public UUID getUuid() { String encodedUuid = getElementContent(UUID_ELEMENT_NAME, element); return Helpers.uuidFromBase64(encodedUuid); }
public static Date fromString(String string) { if (string.equals("${creationDate}")) { return new Date(); } return Helpers.toDate(string); } }
public static byte[] decodeBase64Content (byte[] content, boolean isCompressed) { byte[] value = Base64.decodeBase64(content); if (isCompressed) { return unzipBinaryContent(value); } return value; }
public static String toString(Date date) { return Helpers.fromDate(date); }
public static String encodeBase64Content(byte[] value, boolean isCompressed) { if (!isCompressed) { return Base64.encodeBase64String(value); } return Base64.encodeBase64String(zipBinaryContent(value)); }
@Override public UUID read(InputNode inputNode) throws Exception { String value = inputNode.getValue(); if (value == null || value.equals("")) { return UUID.randomUUID(); } return Helpers.uuidFromBase64(value); }
@Override public Date read(InputNode inputNode) throws Exception { String value = inputNode.getValue(); if (value.equals("${creationDate}")) { return new Date(); } return Helpers.toDate(value); }
@Override public byte[] getBinaryProperty(String name) { for (BinaryField binaryField : delegate.getBinary()){ if (binaryField.getKey().equals(name)){ Integer ref = binaryField.getValue().getRef(); for (Binaries.Binary binary: database.getKeePassFile().getMeta().getBinaries().getBinary()){ if (binary.getID().equals(ref)) { if (binary.getCompressed()) { return Helpers.unzipBinaryContent(binary.getValue()); } return binary.getValue(); } } } } return null; }
@Override public void write(OutputNode outputNode, Date date) throws Exception { outputNode.setValue(Helpers.fromDate(date)); } }
newBin.setValue(Helpers.zipBinaryContent(value)); newBin.setCompressed(true); binaryList.add(newBin);
Crypto(String key) { this.binaryKey = Helpers.decodeBase64Content(key.getBytes()); }
@NotNull static Element setBinaryElementContent(String elementPath, Element parentElement, byte[] value) { try { String b64 = Helpers.encodeBase64Content(value, true); //Find the highest numbered existing content String max = xpath.evaluate("//Binaries/Binary/@ID[not(. < ../../Binary/@ID)][1]", parentElement.getOwnerDocument().getDocumentElement()); Integer newIndex = Integer.valueOf(max) + 1; Element binaries = getElement("//Binaries", parentElement.getOwnerDocument().getDocumentElement(),false); if (binaries == null) { throw new IllegalStateException("Binaries not found"); } Element binary = (Element) binaries.appendChild(binaries.getOwnerDocument().createElement("Binary")); binary.setTextContent(b64); binary.setAttribute("Compressed", "True"); binary.setAttribute("ID", newIndex.toString()); Element result = getElement(elementPath, parentElement, true); result.setAttribute("Ref", newIndex.toString()); return result; } catch (XPathExpressionException e) { throw new IllegalStateException(e); } }
@Override public UUID getUuid() { return Helpers.uuidFromBase64(DomHelper.getElementContent(DomHelper.UUID_ELEMENT_NAME, element)); }