@Override public int compare(Contact contact1, Contact contact2) { int nameComparison = String.CASE_INSENSITIVE_ORDER.compare(contact1.getName(), contact2.getName()); if (nameComparison == 0) { // if the names are the same, compare destination keys String key1 = contact1.getDestination().getKey(); String key2 = contact2.getDestination().getKey(); return key1.compareTo(key2); } else return nameComparison; } }
/** Finds a salt value such that scrypt(nameHash|destination, salt) ends with 10 zero bits */ public static Fingerprint generate(Contact contact) throws GeneralSecurityException { EmailDestination destination = contact.getDestination(); Hash nameHash = EmailIdentity.calculateHash(contact.getName()); return generate(nameHash, destination); }
/** Tests if a new packet replaces an existing one when the names match (it shouldn't) */ @Test public void testStoreExisting() { assertEquals(0, folder.getNumElements()); folder.store(contact1); assertEquals(1, folder.getNumElements()); folder.store(contact2); assertEquals(1, folder.getNumElements()); assertTrue("Contact in the folder was overwritten!", folder.iterator().next().getDestination().toBase64().equals(contact1.getDestination().toBase64())); assertTrue("Contact in the folder was overwritten!", !folder.iterator().next().getDestination().toBase64().equals(contact2.getDestination().toBase64())); folder.store(contact3); assertEquals(2, folder.getNumElements()); }
protected Properties saveToProperties() { SortedProperties properties = new SortedProperties(); int index = 0; for (Contact contact: contacts) { String prefix = "contact" + index + "."; properties.setProperty(prefix + "name", contact.getName()); String base64Dest = contact.getDestination().toBase64(); properties.setProperty(prefix + "destination", base64Dest); String pictureBase64 = contact.getPictureBase64(); properties.setProperty(prefix + "picture", (pictureBase64==null ? "" : pictureBase64)); String text = contact.getText(); properties.setProperty(prefix + "text", (text==null ? "" : text)); index++; } return properties; }
@Test public void toByteArrayAndBack() throws IOException, GeneralSecurityException { WordListAnchor wordLists = new WordListAnchor(); String[] wordListEN = wordLists.getWordList("en"); String[] wordListDE = wordLists.getWordList("de"); byte[] arrayA = contact.toByteArray(); Contact contact2 = new Contact(arrayA); assertEquals(contact.getName(), contact2.getName()); assertEquals(contact.getDestination().toBase64(), contact2.getDestination().toBase64()); assertEquals(contact.getText(), contact2.getText()); assertEquals(contact.getFingerprint().getWords(wordListDE), contact2.getFingerprint().getWords(wordListDE)); assertEquals(contact.getFingerprint().getWords(wordListEN), contact2.getFingerprint().getWords(wordListEN)); assertFalse(contact.getFingerprint().getWords(wordListEN).equals(contact2.getFingerprint().getWords(wordListDE))); assertEquals(contact.getPictureBase64(), contact2.getPictureBase64()); byte[] arrayB = contact2.toByteArray(); assertArrayEquals("The two arrays differ!", arrayA, arrayB); }