Java Code Examples for net.openhft.chronicle.bytes.NativeBytesStore#from()

The following examples show how to use net.openhft.chronicle.bytes.NativeBytesStore#from() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: EasyBoxTest.java    From Chronicle-Salt with Apache License 2.0 6 votes vote down vote up
@Test
public void bulkEncryptDecrypt() {
    EasyBox.KeyPair alice = EasyBox.KeyPair.generate();
    EasyBox.KeyPair bob = EasyBox.KeyPair.generate();
    EasyBox.Nonce nonce = EasyBox.Nonce.generate();

    BytesStore message = NativeBytesStore.from("Hello World, this is a short message for testing purposes");

    int runs = 10000;
    for (int t = 0; t < 3; t++) {
        BytesStore cipher = EasyBox.encrypt(message, nonce, bob.publicKey, alice.secretKey);

        BytesStore clear = EasyBox.decrypt(cipher, nonce, alice.publicKey, bob.secretKey);
        assertTrue(Arrays.equals(message.toByteArray(), clear.toByteArray()));

        message = cipher;
        nonce.next();
    }
}
 
Example 2
Source File: EasyBoxTest.java    From Chronicle-Salt with Apache License 2.0 6 votes vote down vote up
@Test
public void bulkEncryptDecryptShared() {
    EasyBox.KeyPair alice = EasyBox.KeyPair.generate();
    EasyBox.KeyPair bob = EasyBox.KeyPair.generate();

    EasyBox.SharedKey shared = EasyBox.SharedKey.precalc(alice.publicKey, bob.secretKey);
    EasyBox.Nonce nonce = EasyBox.Nonce.generate();

    BytesStore message = NativeBytesStore.from("Hello World, this is a short message for testing purposes");

    int runs = 10000;
    for (int t = 0; t < 3; t++) {
        BytesStore cipher = EasyBox.encryptShared(message, nonce, shared);

        BytesStore clear = EasyBox.decryptShared(cipher, nonce, shared);
        assertTrue(Arrays.equals(message.toByteArray(), clear.toByteArray()));

        message = cipher;
        nonce.next();
    }
}
 
Example 3
Source File: SignatureTest.java    From Chronicle-Salt with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiPart2() {
    BytesStore message1 = NativeBytesStore.from("Message part1");
    BytesStore message2 = NativeBytesStore.from("Message part2");
    BytesStore message3 = NativeBytesStore.from("Message part3");

    Signature.KeyPair keys = Signature.KeyPair.deterministic(123);

    Signature.MultiPart multi = new Signature.MultiPart();
    multi.add(message1);
    multi.add(message2);
    multi.add(message3);
    BytesStore signature = multi.sign(keys.secretKey.store);

    assertEquals(
            "FE7EBF26E92709DB6DC2953F93E757883627CA0956685392E2173774A051ABF5"
                    + "12CB6791D42F13F5C672B226731EF9263284502BC64BD6FDC8858B4BB49CA006",
            DatatypeConverter.printHexBinary(signature.toByteArray()));

    Signature.MultiPart recv = new Signature.MultiPart();
    recv.add(message1);
    recv.add(message2);
    recv.add(message3);
    recv.verify(signature, keys.publicKey.store);
}
 
Example 4
Source File: EasyBoxTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void testEasyBox3() {
    System.out.println("sodium.version= " + Sodium.SODIUM.sodium_version_string());
    BytesStore message = NativeBytesStore.from("test");

    EasyBox.KeyPair alice = EasyBox.KeyPair.generate();
    EasyBox.KeyPair bob = EasyBox.KeyPair.generate();
    EasyBox.Nonce nonce = EasyBox.Nonce.generate();

    BytesStore cipherText = EasyBox.encrypt(null, message, nonce.store, bob.publicKey.store, alice.secretKey.store);
    BytesStore message2 = EasyBox.decrypt(null, cipherText, nonce.store, alice.publicKey.store, bob.secretKey.store);

    assertTrue(Arrays.equals(message.toByteArray(), message2.toByteArray()));
}
 
Example 5
Source File: SealedBoxTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncryptDecrypt2() {
    SealedBox.KeyPair kp = SealedBox.KeyPair.generate();
    BytesStore message = NativeBytesStore.from("Hello World");

    BytesStore c = SealedBox.encrypt(message, kp.publicKey);
    BytesStore message2 = SealedBox.decrypt(c, kp.publicKey, kp.secretKey);

    assertTrue(Arrays.equals(message.toByteArray(), message2.toByteArray()));
}
 
Example 6
Source File: EasyBoxTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void testEasyBox() {
    System.out.println("sodium.version= " + Sodium.SODIUM.sodium_version_string());
    BytesStore message = NativeBytesStore.from("test");

    EasyBox.KeyPair alice = EasyBox.KeyPair.generate();
    EasyBox.KeyPair bob = EasyBox.KeyPair.generate();
    EasyBox.Nonce nonce = EasyBox.Nonce.generate();

    BytesStore cipherText = EasyBox.encrypt(null, message, nonce, bob.publicKey, alice.secretKey);
    BytesStore message2 = EasyBox.decrypt(null, cipherText, nonce, alice.publicKey, bob.secretKey);

    assertTrue(Arrays.equals(message.toByteArray(), message2.toByteArray()));
}
 
Example 7
Source File: EasyBoxTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void testEasyBoxMessageDeterministicShared() {
    BytesStore message = NativeBytesStore
            .from("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et "
                    + "dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip "
                    + "ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu "
                    + "fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt "
                    + "mollit anim id est laborum.");

    EasyBox.KeyPair alice = EasyBox.KeyPair.deterministic(123);
    EasyBox.KeyPair bob = EasyBox.KeyPair.deterministic(456);

    EasyBox.SharedKey sharedA = EasyBox.SharedKey.precalc(bob.publicKey, alice.secretKey);
    EasyBox.SharedKey sharedB = EasyBox.SharedKey.precalc(alice.publicKey, bob.secretKey);

    assertEquals(DatatypeConverter.printHexBinary(sharedA.store.toByteArray()), DatatypeConverter.printHexBinary(sharedB.store.toByteArray()));

    EasyBox.Nonce nonce = EasyBox.Nonce.deterministic(789);

    String expected = "970DC924AFCBD44DD20FA514CD328575BD70B483E3C88FE4C20F1F744CE18A8E6D543C3CA3D033B36D9341F32A34A098797762503ECEB8"
            + "C5D24E0C290CA08F5B7A188D3E0E4AB55D767A31F89546171BDE69BC64AFF116DD07A196D9B5D41FF6F7D273B92705450213A2BDCBA3A7"
            + "808C96646E8F0410BF5D2BDC05C71E3A35737D3276400372C0FC53631B445B94F2AB3E4DF55B1BE3B1373BAE36E5A44F0E4B046FF2FCBA"
            + "E8C55E89FDDE6468B0F908176745BA6D1DA788EF08546CC2A675067A6C3A907C765D97EF1C2184B9B748F6081F7168C57BBFABADB0357E"
            + "892A71DCED7867E4D3225A96598FBA9E3771509493C85085DE5ED05A597A45B3B7EF0A3C3FC8A1062AD67C48407C7B7DC0522167FE8295"
            + "DA52A0D311CE995159728FB51F1D2DFEB738442A6FD7C8CA4C9FEB2ED0584D12C5359D0EA558CFC72545CBB821754959DA35F6839866E6"
            + "C04A400E0FE9D8689426E9F1EAE1D77F93026D7B9E19A56353F59EFF980090053C09FB7CCF7438366AD0B9E9DB77042C0491B540646D02"
            + "FE8BB5C3A310B126C932290DD4885915F379A475E52B4025ED495B59BAC92C5487827065B26732A52545E5FCF044DBB3D5F827CA6B7CDF"
            + "E28062EC726BA7A0B0C73C058EDC66485C69663481";

    long msglen = message.readRemaining();

    BytesStore cipherText = EasyBox.encryptShared(message, nonce, sharedA);
    assertTrue(expected.equals(DatatypeConverter.printHexBinary(cipherText.toByteArray())));

    long cipherlen = cipherText.readRemaining();
    assertTrue(msglen + 16 == cipherlen); // 16 = CRYPTO_BOX_MACBYTES

    BytesStore message2 = EasyBox.decryptShared(cipherText, nonce, sharedB);
    assertTrue(Arrays.equals(message.toByteArray(), message2.toByteArray()));
}
 
Example 8
Source File: SHA2Test.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiPart512() {
    BytesStore message1 = NativeBytesStore.from("Message part1");
    BytesStore message2 = NativeBytesStore.from("Message part2");
    BytesStore message3 = NativeBytesStore.from("Message part3");

    SHA2.MultiPartSHA512 multi = new SHA2.MultiPartSHA512();
    multi.add(message1);
    multi.add(message2);
    multi.add(message3);
    BytesStore hash = multi.hash();

    assertEquals("D03F370D9C234701370A0323AF9BB5D0E13AEB128C6C14C427DD25B1FFCFA7EB"
            + "B505665AD2C97D989A3F460715D3C688FE04B9FC8AAA3213051486930A3B3876", DatatypeConverter.printHexBinary(hash.toByteArray()));
}
 
Example 9
Source File: SHA2Test.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiPart256() {
    BytesStore message1 = NativeBytesStore.from("Message part1");
    BytesStore message2 = NativeBytesStore.from("Message part2");
    BytesStore message3 = NativeBytesStore.from("Message part3");

    SHA2.MultiPartSHA256 multi = new SHA2.MultiPartSHA256();
    multi.add(message1);
    multi.add(message2);
    multi.add(message3);
    BytesStore hash = multi.hash();

    assertEquals("34A26FB451F7A08C239F48D8086DCD1628FD8BDE5F54E4600EE91BA5BEBC21AB", DatatypeConverter.printHexBinary(hash.toByteArray()));
}
 
Example 10
Source File: SealedBoxTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testDecryptFailsFlippedKeys() {
    SealedBox.KeyPair kp = SealedBox.KeyPair.generate();
    BytesStore message = NativeBytesStore.from("Hello World");

    BytesStore c = SealedBox.encrypt(null, message, kp.publicKey);
    // NB: this - intentionally - won't compile. Need to force with the "unsafe" interface
    // SealedBox.decrypt(cipherText, kp.secretKey, kp.publicKey);
    SealedBox.decrypt(null, c, kp.secretKey.store, kp.publicKey.store);
}
 
Example 11
Source File: SealedBoxTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncryptDecrypt3() {
    SealedBox.KeyPair kp = SealedBox.KeyPair.generate();
    BytesStore message = NativeBytesStore.from("Hello World");

    BytesStore c = SealedBox.encrypt(null, message, kp.publicKey.store);
    BytesStore message2 = SealedBox.decrypt(null, c, kp.publicKey.store, kp.secretKey.store);

    assertTrue(Arrays.equals(message.toByteArray(), message2.toByteArray()));
}
 
Example 12
Source File: SignatureTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test(expected = IllegalStateException.class)
public void testVerifyFailsFlippedKeys() {
    BytesStore message = NativeBytesStore.from("Hello World");

    Signature.KeyPair keys = Signature.KeyPair.generate();

    BytesStore signed = Signature.sign(message, keys.secretKey);

    // NB: this - intentionally - won't compile. Need to force with the "unsafe" interface
    // Signature.verify(signed, keys.publicKey);
    Signature.verify(null, signed, keys.secretKey.store);
}
 
Example 13
Source File: SignatureTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void testSignVerify3() {
    BytesStore message = NativeBytesStore.from("test message");

    Signature.KeyPair keys = Signature.KeyPair.generate();

    BytesStore signed = Signature.sign(null, message, keys.secretKey.store);
    BytesStore unsigned = Signature.verify(null, signed, keys.publicKey.store);

    assertTrue(Arrays.equals(message.toByteArray(), unsigned.toByteArray()));
}
 
Example 14
Source File: SignatureTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void testSignVerify2() {
    BytesStore message = NativeBytesStore.from("test message");

    Signature.KeyPair keys = Signature.KeyPair.generate();

    BytesStore signed = Signature.sign(message, keys.secretKey);
    BytesStore unsigned = Signature.verify(signed, keys.publicKey);

    assertTrue(Arrays.equals(message.toByteArray(), unsigned.toByteArray()));
}
 
Example 15
Source File: SignatureTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void testSignVerify() {
    BytesStore message = NativeBytesStore.from("test message");

    Signature.KeyPair keys = Signature.KeyPair.generate();

    BytesStore signed = Signature.sign(null, message, keys.secretKey);
    BytesStore unsigned = Signature.verify(null, signed, keys.publicKey);

    assertTrue(Arrays.equals(message.toByteArray(), unsigned.toByteArray()));
}
 
Example 16
Source File: SignatureTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void testKeyPairLongSeed() {
    BytesStore seed = NativeBytesStore.from("01234567890123456789012345678901");
    Signature.KeyPair kp = Signature.KeyPair.deterministic(seed);

    assertEquals("7BC3079518ED11DA0336085BF6962920FF87FB3C4D630A9B58CB6153674F5DD6",
            DatatypeConverter.printHexBinary(kp.publicKey.store.toByteArray()));
}
 
Example 17
Source File: EasyBoxTest.java    From Chronicle-Salt with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testNonceDeterministicTooShort() {
    BytesStore seed = NativeBytesStore.from("0123456789012345678901234567");
    EasyBox.Nonce nonce = EasyBox.Nonce.deterministic(seed);
}
 
Example 18
Source File: EasyBoxTest.java    From Chronicle-Salt with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testNonceDeterministicTooLong() {
    BytesStore seed = NativeBytesStore.from("0123456789012345678901234567890123456789");
    EasyBox.Nonce nonce = EasyBox.Nonce.deterministic(seed);
}
 
Example 19
Source File: SignatureTest.java    From Chronicle-Salt with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testKeyPairDeterministicTooLong() {
    BytesStore seed = NativeBytesStore.from("0123456789012345678901234567890123456789");
    Signature.KeyPair kp = Signature.KeyPair.deterministic(seed);
}
 
Example 20
Source File: SignatureTest.java    From Chronicle-Salt with Apache License 2.0 4 votes vote down vote up
@Test(expected = IllegalArgumentException.class)
public void testKeyPairDeterministicTooShort() {
    BytesStore seed = NativeBytesStore.from("0123456789012345678901234567");
    Signature.KeyPair kp = Signature.KeyPair.deterministic(seed);
}