net.openhft.chronicle.bytes.BytesStore Java Examples

The following examples show how to use net.openhft.chronicle.bytes.BytesStore. 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: VanillaChronicleMap.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
@NotNull
@Override
public ExternalMapQueryContext<K, V, ?> queryContext(
        BytesStore keyBytes, long offset, long size) {
    Objects.requireNonNull(keyBytes);
    QueryContextInterface<K, V, R> c = mapContext();
    try {
        c.initInputKey(c.getInputKeyBytesAsData(keyBytes, offset, size));
        return c;
    } catch (Throwable throwable) {
        try {
            c.close();
        } catch (Throwable t) {
            throwable.addSuppressed(t);
        }
        throw throwable;
    }
}
 
Example #2
Source File: VanillaChronicleHash.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
public final void initBeforeMapping(
        File file, RandomAccessFile raf, long headerEnd, boolean recover) throws IOException {
    this.file = file;
    this.raf = raf;
    this.headerSize = roundUpMapHeaderSize(headerEnd);
    if (!createdOrInMemory) {
        // This block is for reading segmentHeadersOffset before main mapping
        // After the mapping globalMutableState value's bytes are reassigned
        ByteBuffer globalMutableStateBuffer =
                ByteBuffer.allocate((int) globalMutableState.maxSize());
        FileChannel fileChannel = raf.getChannel();
        while (globalMutableStateBuffer.remaining() > 0) {
            if (fileChannel.read(globalMutableStateBuffer,
                    this.headerSize + GLOBAL_MUTABLE_STATE_VALUE_OFFSET +
                            globalMutableStateBuffer.position()) == -1) {
                throw throwRecoveryOrReturnIOException(file, "truncated", recover);
            }
        }
        globalMutableStateBuffer.flip();
        //noinspection unchecked
        globalMutableState.bytesStore(BytesStore.wrap(globalMutableStateBuffer), 0,
                globalMutableState.maxSize());
    }
}
 
Example #3
Source File: ThroughputMain.java    From Chronicle-Queue with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("restriction")
    private static long writeMessages(long address, long canWrite, int writeCount, BytesStore nbs) {
        long length = 0;
        long count = 0;
//        writeCount = writeCount == 1 ? 1 : ThreadLocalRandom.current().nextInt(writeCount-1)+1;
        long fromAddress = nbs.addressForRead(0);
        while (writeCount > count && length + 4 + size <= canWrite) {
            UnsafeMemory.UNSAFE.copyMemory(fromAddress, address + 4, size);
            UnsafeMemory.UNSAFE.putOrderedInt(null, address, size);
            address += 4 + size;
            length += 4 + size;
            count++;
        }
//        System.out.println("w "+count+" "+length);
        return (count << 32) | length;
    }
 
Example #4
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 #5
Source File: SignatureTest.java    From Chronicle-Salt with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiPart() {
    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);

    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);
}
 
Example #6
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 #7
Source File: SignatureTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void extractTest2() {
    BytesStore seed = NativeBytesStore.from("01234567890123456789012345678901");
    Signature.KeyPair keys = Signature.KeyPair.deterministic(seed);

    BytesStore seed2 = keys.secretKey.extractSeed();
    assertEquals("3031323334353637383930313233343536373839303132333435363738393031", DatatypeConverter.printHexBinary(seed2.toByteArray()));

    BytesStore pk = keys.secretKey.extractPublicKey();
    assertEquals("7BC3079518ED11DA0336085BF6962920FF87FB3C4D630A9B58CB6153674F5DD6", DatatypeConverter.printHexBinary(pk.toByteArray()));

    assertTrue(Arrays.equals(keys.publicKey.store.toByteArray(), pk.toByteArray()));
}
 
Example #8
Source File: SignatureTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void extractTest() {
    Signature.KeyPair keys = Signature.KeyPair.deterministic(123);

    BytesStore seed = keys.secretKey.extractSeed();
    assertEquals("7B00000000000000000000000000000000000000000000000000000000000000", DatatypeConverter.printHexBinary(seed.toByteArray()));

    BytesStore pk = keys.secretKey.extractPublicKey();
    assertEquals("9B37EDB59199672751E762C5200873E98619EB210AD241862940C740929AF814", DatatypeConverter.printHexBinary(pk.toByteArray()));

    assertTrue(Arrays.equals(keys.publicKey.store.toByteArray(), pk.toByteArray()));
}
 
Example #9
Source File: VanillaChronicleHash.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
private void appendBulkData(int firstBulkToMapIndex, int upToBulkIndex, BytesStore extraStore,
                            long offsetWithinMapping) {
    TierBulkData firstMappedBulkData = new TierBulkData(extraStore, offsetWithinMapping);
    tierBulkOffsets.add(firstMappedBulkData);
    for (int bulkIndex = firstBulkToMapIndex + 1; bulkIndex <= upToBulkIndex; bulkIndex++) {
        tierBulkOffsets.add(new TierBulkData(firstMappedBulkData,
                offsetWithinMapping += tierBulkSizeInBytes));
    }
}
 
Example #10
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 #11
Source File: SignatureTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void testSignatureDeterministic() {
    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.");

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

    String expected = "1F02DE4C6263043CD7F6071263FA22BA7330D4F6383F15E28C7020E2C9D4A46F5B896FCBC9020B43741BBF1830246F4B7BF"
            + "5425200DC5405F1A1DE8AF241640A4C6F72656D20697073756D20646F6C6F722073697420616D65742C20636F6E73656374"
            + "657475722061646970697363696E6720656C69742C2073656420646F20656975736D6F642074656D706F7220696E6369646"
            + "964756E74207574206C61626F726520657420646F6C6F7265206D61676E6120616C697175612E20557420656E696D206164"
            + "206D696E696D2076656E69616D2C2071756973206E6F737472756420657865726369746174696F6E20756C6C616D636F206"
            + "C61626F726973206E69736920757420616C697175697020657820656120636F6D6D6F646F20636F6E7365717561742E2044"
            + "756973206175746520697275726520646F6C6F7220696E20726570726568656E646572697420696E20766F6C75707461746"
            + "52076656C697420657373652063696C6C756D20646F6C6F726520657520667567696174206E756C6C612070617269617475"
            + "722E204578636570746575722073696E74206F6363616563617420637570696461746174206E6F6E2070726F6964656E742"
            + "C2073756E7420696E2063756C706120717569206F666669636961206465736572756E74206D6F6C6C697420616E696D2069"
            + "6420657374206C61626F72756D2E";

    long msglen = message.readRemaining();

    BytesStore signed = Signature.sign(message, keys.secretKey);
    assertTrue(expected.equals(DatatypeConverter.printHexBinary(signed.toByteArray())));

    long signedlen = signed.readRemaining();
    assertTrue(msglen + 64 == signedlen); // 16 = CRYPTO_BOX_MACBYTES

    BytesStore message2 = Signature.verify(signed, keys.publicKey);
    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
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 #13
Source File: SHA2Test.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void testMultiPartREADME() {
    BytesStore message1 = NativeBytesStore.from("abcdefgh");
    BytesStore message2 = NativeBytesStore.from("ijklmnop");
    BytesStore message3 = NativeBytesStore.from("qrstuvwxyz");

    // Initialise a MultiPartSHA256 wrapper
    SHA2.MultiPartSHA256 multi256 = new SHA2.MultiPartSHA256();
    multi256.add(message1);
    multi256.add(message2);
    multi256.add(message3);

    // Generate the single SHA-256 hash of the set of messages
    BytesStore hash256 = multi256.hash();

    // Initialise a MultiPartSHA512 wrapper
    SHA2.MultiPartSHA512 multi512 = new SHA2.MultiPartSHA512();
    multi512.add(message1);
    multi512.add(message2);
    multi512.add(message3);

    // Generate the single SHA-512 hash of the set of messages
    BytesStore hash512 = multi512.hash();

    System.out.println("SHA256: " + DatatypeConverter.printHexBinary(hash256.toByteArray()));
    System.out.println("SHA512: " + DatatypeConverter.printHexBinary(hash512.toByteArray()));

    BytesStore message = NativeBytesStore.from("abcdefghijklmnopqrstuvwxyz");

    BytesStore hash1 = SHA2.sha256(message);
    assertTrue(Arrays.equals(hash1.toByteArray(), hash256.toByteArray()));

    BytesStore hash2 = SHA2.sha512(message);
    assertTrue(Arrays.equals(hash2.toByteArray(), hash512.toByteArray()));
}
 
Example #14
Source File: Ed25519.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
boolean verify(BytesStore<?, ?> sigAndMsg, BytesStore<?, ?> publicKey) {
    int length = sigAndMsg.length();
    buffer.ensureCapacity(length);
    long bufferAddress = this.buffer.addressForWrite(0);
    long sigAndMsgAddress = sigAndMsg.addressForRead(sigAndMsg.readPosition());
    long publicKeyAddress = publicKey.addressForRead(publicKey.readLimit() - Ed25519.PUBLIC_KEY_LENGTH);
    int ret = SODIUM.crypto_sign_ed25519_open(bufferAddress, sigLen, sigAndMsgAddress, (int) sigAndMsg.readRemaining(), publicKeyAddress);
    long l = sigLen.longValue();
    assert l <= length;
    return ret == 0;
}
 
Example #15
Source File: Ed25519.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
void sign(BytesStore sigAndMsg, BytesStore<?, ?> secretKey) {
    int msgLen = (int) sigAndMsg.readRemaining() - Ed25519.SIGNATURE_LENGTH;
    long signatureAddress = sigAndMsg.addressForRead(sigAndMsg.readPosition());
    long messageAddress = sigAndMsg.addressForRead(sigAndMsg.readPosition() + SIGNATURE_LENGTH);
    long secretKeyAddress = secretKey.addressForRead(secretKey.readPosition());
    checkValid(SODIUM.crypto_sign_ed25519(signatureAddress, sigLen, messageAddress, msgLen, secretKeyAddress), "Unable to sign");
    assert sigLen.longValue() == sigAndMsg.readRemaining();
}
 
Example #16
Source File: Ed25519.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
void sign(Bytes<?> sigAndMsg, BytesStore<?, ?> message, BytesStore<?, ?> secretKey) {
    int msgLen = Math.toIntExact(message.readRemaining());
    long signature = sigAndMsg.addressForWrite(sigAndMsg.writePosition());
    long messageAddress = message.addressForRead(message.readPosition());
    long secretKeyAddress = secretKey.addressForRead(secretKey.readPosition());
    checkValid(SODIUM.crypto_sign_ed25519(signature, sigLen, messageAddress, msgLen, secretKeyAddress), "Unable to sign");
    long bytesToSkip = sigLen.longValue();
    sigAndMsg.writeSkip(bytesToSkip);
}
 
Example #17
Source File: VanillaChronicleHash.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
private void allocateTierBulks(int upToBulkIndex) {
    int firstBulkToAllocateIndex = tierBulkOffsets.size();
    int bulksToAllocate = upToBulkIndex + 1 - firstBulkToAllocateIndex;
    long allocationSize = bulksToAllocate * tierBulkSizeInBytes;
    BytesStore extraStore = nativeBytesStoreWithFixedCapacity(allocationSize);
    appendBulkData(firstBulkToAllocateIndex, upToBulkIndex, extraStore, 0);
}
 
Example #18
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 #19
Source File: EasyBox.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
private Nonce(BytesStore store, long id) {
    BytesStore seed = Bytes.allocateDirect(RANDOMBYTES_SEEDBYTES);
    seed.writeLong(0, id);

    this.store = Sodium.Util.setSize(store, CRYPTO_BOX_NONCEBYTES);
    SODIUM.randombytes_buf_deterministic(this.store.addressForWrite(0), CRYPTO_BOX_NONCEBYTES, seed.addressForWrite(0));
}
 
Example #20
Source File: EasyBoxTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void testKeyPairLongSeed() {
    BytesStore seed = NativeBytesStore.from("01234567890123456789012345678901");
    EasyBox.KeyPair kp = EasyBox.KeyPair.deterministic(seed);

    assertEquals("11BF74568407F0D337369E0F6A0375F5420B53B649CF9C9E6A44E53769A75C71",
            DatatypeConverter.printHexBinary(kp.publicKey.store.toByteArray()));
}
 
Example #21
Source File: StateMachineData.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
@Override
public void bytesStore(BytesStore bytes, long offset, long size) {
    if (size != 16)
        throw new IllegalArgumentException();
    this.bs = bytes;
    this.offset = offset;
}
 
Example #22
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 #23
Source File: EasyBoxTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void testEasyBox2() {
    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(message, nonce, bob.publicKey, alice.secretKey);
    BytesStore message2 = EasyBox.decrypt(cipherText, nonce, alice.publicKey, bob.secretKey);

    assertTrue(Arrays.equals(message.toByteArray(), message2.toByteArray()));
}
 
Example #24
Source File: EasyBoxTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void testEasyBoxMessageDeterministic() {
    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.Nonce nonce = EasyBox.Nonce.deterministic(789);

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

    long msglen = message.readRemaining();

    BytesStore cipherText = EasyBox.encrypt(message, nonce, bob.publicKey, alice.secretKey);
    assertTrue(expected.equals(DatatypeConverter.printHexBinary(cipherText.toByteArray())));

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

    BytesStore message2 = EasyBox.decrypt(cipherText, nonce, alice.publicKey, bob.secretKey);
    assertTrue(Arrays.equals(message.toByteArray(), message2.toByteArray()));
}
 
Example #25
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 #26
Source File: SealedBoxTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncryptDecrypt() {
    SealedBox.KeyPair kp = SealedBox.KeyPair.generate();
    BytesStore message = NativeBytesStore.from("Hello World");

    long msglen = message.readRemaining();
    BytesStore c = SealedBox.encrypt(null, message, kp.publicKey);

    long clen = c.readRemaining();
    assertTrue(msglen + 48 == clen); // 48 = CRYPTO_BOX_SEALBYTES

    BytesStore message2 = SealedBox.decrypt(null, c, kp.publicKey, kp.secretKey);
    assertTrue(Arrays.equals(message.toByteArray(), message2.toByteArray()));
}
 
Example #27
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 #28
Source File: SingleChronicleQueueBuilder.java    From Chronicle-Queue with Apache License 2.0 5 votes vote down vote up
public SingleChronicleQueueBuilder codingSuppliers(@Nullable
                                                           Supplier<BiConsumer<BytesStore, Bytes>> encodingSupplier,
                                                   @Nullable Supplier<BiConsumer<BytesStore, Bytes>> decodingSupplier) {
    if ((encodingSupplier == null) != (decodingSupplier == null))
        throw new UnsupportedOperationException("Both encodingSupplier and decodingSupplier must be set or neither");
    this.encodingSupplier = encodingSupplier;
    this.decodingSupplier = decodingSupplier;
    return this;
}
 
Example #29
Source File: VanillaChronicleHash.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
private void allocateTierBulk() throws IOException {
    int allocatedExtraTierBulks = globalMutableState.getAllocatedExtraTierBulks();

    mapTierBulks(allocatedExtraTierBulks);

    long firstTierIndex = extraTierIndexToTierIndex(allocatedExtraTierBulks * tiersInBulk);
    BytesStore tierBytesStore = tierBytesStore(firstTierIndex);
    long firstTierOffset = tierBytesOffset(firstTierIndex);
    if (tierBulkInnerOffsetToTiers > 0) {
        // These bytes are bit sets in Replicated version
        tierBytesStore.zeroOut(firstTierOffset - tierBulkInnerOffsetToTiers, firstTierOffset);
    }

    long lastTierIndex = firstTierIndex + tiersInBulk - 1;
    linkAndZeroOutFreeTiers(firstTierIndex, lastTierIndex);

    // see HCOLL-397
    if (persisted()) {
        long address = tierBytesStore.addressForRead(firstTierOffset - tierBulkInnerOffsetToTiers);
        long endAddress = tierBytesStore.addressForRead(tierBytesOffset(lastTierIndex)) + tierSize;
        long length = endAddress - address;
        msync(address, length);
    }

    // after we are sure the new bulk is initialized, update the global mutable state
    globalMutableState.setAllocatedExtraTierBulks(allocatedExtraTierBulks + 1);
    globalMutableState.setFirstFreeTierIndex(firstTierIndex);
    globalMutableState.addDataStoreSize(tierBulkSizeInBytes);
}
 
Example #30
Source File: SbeBufferImpl.java    From java-cme-mdp3-handler with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void copyFrom(SbeBuffer buffer) {
    final SbeBufferImpl bufferImpl = (SbeBufferImpl) buffer;
    final long fromLength = bufferImpl.bytes.length();
    if (this.bytes == null) {
        this.bytes = BytesStore.wrap(ByteBuffer.allocateDirect((int) bufferImpl.bytes.realCapacity()).order(ByteOrder.LITTLE_ENDIAN));
    }
    final long toLength = bytes.length();
    if(toLength < fromLength) {
        bytes.write(0, bufferImpl.bytes, 0, toLength);
    } else {
        bytes.write(0, bufferImpl.bytes, 0, fromLength);
    }
}