Java Code Examples for net.openhft.chronicle.bytes.Bytes#readPositionRemaining()

The following examples show how to use net.openhft.chronicle.bytes.Bytes#readPositionRemaining() . 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: BatchSha256Rc4Test.java    From Chronicle-Salt with Apache License 2.0 6 votes vote down vote up
@Test
public void testHash() {
    if ((testCounter % 250) == 0) {
        long newTime = System.currentTimeMillis();
        System.out.println("Executing test number " + testCounter + " for data size " + size + " time since last log "
                + String.format("%.2f", ((newTime - timePassed) / 1000.0)) + " sec(s)");
        timePassed = newTime;
    }
    testCounter++;
    Bytes<?> bytesMessage = testDataBytes;
    bytesMessage.readPositionRemaining(0, size);
    Bytes<?> sha256Actual = hash256Bytes.get();
    sha256Actual.writePosition(0);
    SHA2.appendSha256(sha256Actual, bytesMessage);
    sha256Actual.readPosition(0);

    Bytes<?> sha256Expected = bft.fromHex(sha256);
    sha256Expected.readPosition(0);

    assertEquals(sha256Expected.toHexString(SHA2.HASH_SHA256_BYTES), sha256Actual.toHexString(SHA2.HASH_SHA256_BYTES));
    sha256Expected.releaseLast();
}
 
Example 2
Source File: BatchSha256Sha512RandomTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void testHash() {
    Bytes<?> bytesMessage = bft.fromHex(data);
    bytesMessage.readPosition(0);
    bytesMessage.readPositionRemaining(0, size);

    Bytes<?> actualSha256 = hash256Bytes.get();
    actualSha256.writePosition(0);
    SHA2.appendSha256(actualSha256, bytesMessage);
    actualSha256.readPosition(0);
    Bytes<?> expectedSha256 = bft.fromHex(sha256);
    actualSha256.readPosition(0);
    assertEquals(expectedSha256.toHexString(), actualSha256.toHexString());
    expectedSha256.releaseLast();

    bytesMessage.readPositionRemaining(0, size);
    Bytes<?> actualSha512 = hash512Bytes.get();
    actualSha512.writePosition(0);
    SHA2.appendSha512(actualSha512, bytesMessage);
    actualSha512.readPosition(0);
    Bytes<?> expectedSha512 = bft.fromHex(sha512);
    actualSha512.readPosition(0);
    assertEquals(expectedSha512.toHexString(), actualSha512.toHexString());
    expectedSha512.releaseLast();

    bytesMessage.releaseLast();
}
 
Example 3
Source File: SodiumTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void crypto_box_curve25519xsalsa20poly1305_keypair() {
    Bytes secretKey0 = bytesWithZeros(ED25519_SECRETKEY_BYTES);

    assertEquals(0, SODIUM.crypto_box_curve25519xsalsa20poly1305_keypair(secretKey0.addressForWrite(32), secretKey0.addressForWrite(0)));

    checkZeros(secretKey0);

    secretKey0.readPositionRemaining(0, 64);
    System.out.println(secretKey0.toHexString());
    checkPseudoRandom(secretKey0, ED25519_SECRETKEY_BYTES);

    Bytes publicKey = bytesWithZeros(ED25519_PUBLICKEY_BYTES);
    Bytes secretKey = bytesWithZeros(ED25519_SECRETKEY_BYTES);

    assertEquals(0,
            SODIUM.crypto_sign_ed25519_seed_keypair(publicKey.addressForWrite(0), secretKey.addressForWrite(0), secretKey0.addressForRead(0)));
    checkZeros(publicKey);
    checkZeros(secretKey);
    checkPseudoRandom(publicKey, ED25519_PUBLICKEY_BYTES);
    checkPseudoRandom(secretKey, ED25519_SECRETKEY_BYTES);
    // publicKey.readPositionRemaining(0, 32);
    // System.out.println(publicKey.toHexString());
    // secretKey.readPositionRemaining(0, 64);
    // System.out.println(secretKey.toHexString());

}
 
Example 4
Source File: SodiumTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void signAndVerify() {
    final String SIGN_PRIVATE = "b18e1d0045995ec3d010c387ccfeb984d783af8fbb0f40fa7db126d889f6dadd";

    Bytes publicKey = bytesWithZeros(32);
    Bytes secretKey = bytesWithZeros(64);
    Bytes privateKey = fromHex(SIGN_PRIVATE);
    assertEquals(0,
            SODIUM.crypto_sign_ed25519_seed_keypair(publicKey.addressForWrite(0), secretKey.addressForWrite(0), privateKey.addressForRead(0)));
    publicKey.readPositionRemaining(0, 32);
    secretKey.readPositionRemaining(0, 64);

    Bytes emptyMsg = bytesWithZeros(0);
    Bytes sigAndMsg = bytesWithZeros(ED25519_SECRETKEY_BYTES);

    assertEquals(0, SODIUM.crypto_sign_ed25519(sigAndMsg.addressForWrite(0), new LongLongByReference(0), emptyMsg.addressForRead(0), 0,
            secretKey.addressForRead(0)));

    checkZeros(emptyMsg);
    checkZeros(secretKey);
    checkZeros(sigAndMsg);

    Bytes buffer = bytesWithZeros(ED25519_SECRETKEY_BYTES);

    assertEquals(0, SODIUM.crypto_sign_ed25519_open(buffer.addressForWrite(0), new LongLongByReference(0), sigAndMsg.addressForRead(0), 0 + 64,
            publicKey.addressForRead(0)));

    sigAndMsg.readPositionRemaining(0, 64);
    System.out.println(sigAndMsg.toHexString());
}
 
Example 5
Source File: SodiumTest.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void signAndVerify2() {
    final String SIGN_PRIVATE = "b18e1d0045995ec3d010c387ccfeb984d783af8fbb0f40fa7db126d889f6dadd";
    final String SIGN_MESSAGE = "916c7d1d268fc0e77c1bef238432573c39be577bbea0998936add2b50a653171"
            + "ce18a542b0b7f96c1691a3be6031522894a8634183eda38798a0c5d5d79fbd01"
            + "dd04a8646d71873b77b221998a81922d8105f892316369d5224c9983372d2313"
            + "c6b1f4556ea26ba49d46e8b561e0fc76633ac9766e68e21fba7edca93c4c7460" + "376d7f3ac22ff372c18f613f2ae2e856af40";
    final String SIGN_SIGNATURE = "6bd710a368c1249923fc7a1610747403040f0cc30815a00f9ff548a896bbda0b"
            + "4eb2ca19ebcf917f0f34200a9edbad3901b64ab09cc5ef7b9bcc3c40c0ff7509";

    Bytes publicKey = bytesWithZeros(32);
    Bytes secretKey = bytesWithZeros(64);
    Bytes privateKey = fromHex(SIGN_PRIVATE);
    assertEquals(0,
            SODIUM.crypto_sign_ed25519_seed_keypair(publicKey.addressForWrite(0), secretKey.addressForWrite(0), privateKey.addressForRead(0)));
    publicKey.readPositionRemaining(0, 32);
    secretKey.readPositionRemaining(0, 64);
    // System.out.println(publicKey.toHexString());
    // System.out.println(secretKey.toHexString());

    Bytes sigAndMsg2 = fromHex(SIGN_SIGNATURE);
    Bytes message = fromHex(SIGN_MESSAGE);
    Bytes sigAndMsg = fromHex(64 + (SIGN_MESSAGE.length() / 2), "");

    LongLongByReference sigLen = new LongLongByReference(0);
    assertEquals(0, SODIUM.crypto_sign_ed25519(sigAndMsg.addressForWrite(0), sigLen, message.addressForRead(0), (int) message.readRemaining(),
            secretKey.addressForRead(0)));
    checkZeros(sigAndMsg);
    assertEquals(210, sigLen.longValue());
    sigAndMsg.readLimit(64);
    assertEquals(sigAndMsg2.toHexString(), sigAndMsg.toHexString());

    Bytes buffer = bytesWithZeros(210);

    LongLongByReference bufferLen = new LongLongByReference(0);
    assertEquals(0,
            SODIUM.crypto_sign_ed25519_open(buffer.addressForWrite(0), bufferLen, sigAndMsg.addressForRead(0), 210, publicKey.addressForRead(0)));
    assertEquals(210 - 64, bufferLen.longValue());

}
 
Example 6
Source File: BytesForTesting.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
static void checkPseudoRandom(Bytes bytes, long size) throws java.nio.BufferUnderflowException {
    bytes.readPositionRemaining(0, size);
    int count = 0;
    while (bytes.readRemaining() > 7) {
        count += Long.bitCount(bytes.readLong());
    }
    assertEquals(size * 4, count, size);
}
 
Example 7
Source File: BatchSignAndVerifyEd25519Test.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
@Test
public void signAndVerify() {
    Bytes privateKeyBuffer = null;
    Bytes secretKeyBuffer = null;
    Bytes privateOrSecret = bft.fromHex(privateOrSecretKey);
    if (privateOrSecret.readRemaining() == Ed25519.SECRET_KEY_LENGTH) {
        secretKeyBuffer = privateOrSecret;
    } else {
        privateKeyBuffer = privateOrSecret;
    }

    Bytes publicKeyBuffer = bft.fromHex(publicKey);
    if (secretKeyBuffer == null) {
        secretKeyBuffer = bft.bytesWithZeros(Ed25519.SECRET_KEY_LENGTH);
        Bytes tmpPublicKeyBuffer = bft.bytesWithZeros(Ed25519.PUBLIC_KEY_LENGTH);
        Ed25519.privateToPublicAndSecret(tmpPublicKeyBuffer, secretKeyBuffer, privateKeyBuffer);
        assertEquals(publicKeyBuffer.toHexString(), tmpPublicKeyBuffer.toHexString());
    }
    Bytes messageBuffer = bft.fromHex(message);
    Bytes signExpectedBuffer;
    if (signExpected.length() == 128) {
        signExpectedBuffer = Bytes.wrapForRead(DatatypeConverter.parseHexBinary(signExpected + message));
    } else {
        signExpectedBuffer = Bytes.wrapForRead(DatatypeConverter.parseHexBinary(signExpected));
    }
    Bytes signedMsgBuffer = bft.fromHex(Ed25519.SIGNATURE_LENGTH, message);
    signedMsgBuffer.writePosition(0);
    Ed25519.sign(signedMsgBuffer, messageBuffer, secretKeyBuffer);
    assertEquals(signExpectedBuffer.toHexString(), signedMsgBuffer.toHexString());
    signedMsgBuffer.readPosition(0);
    publicKeyBuffer.readPositionRemaining(0, Ed25519.PUBLIC_KEY_LENGTH);
    assertTrue(Ed25519.verify(signedMsgBuffer, publicKeyBuffer));
}
 
Example 8
Source File: Ed25519Test.java    From Chronicle-Salt with Apache License 2.0 4 votes vote down vote up
@Test
public void signAndVerify() {
    final String SIGN_PRIVATE = "b18e1d0045995ec3d010c387ccfeb984d783af8fbb0f40fa7db126d889f6dadd";

    Bytes publicKey = bytesWithZeros(32);
    Bytes secretKey = bytesWithZeros(64);
    Bytes privateKey = fromHex(SIGN_PRIVATE);
    Ed25519.privateToPublicAndSecret(publicKey, secretKey, privateKey);
    assertEquals(32, publicKey.readRemaining());
    assertEquals(64, secretKey.readRemaining());

    Bytes emptyMsg = bytesWithZeros(0);
    Bytes sigAndMsg0 = bytesWithZeros(ED25519_SECRETKEY_BYTES);
    Bytes sigAndMsg = bytesWithZeros(ED25519_SECRETKEY_BYTES);

    Ed25519.sign(sigAndMsg0, emptyMsg, secretKey);

    assertEquals(0, SODIUM.crypto_sign_ed25519(sigAndMsg.addressForWrite(0), new LongLongByReference(0), emptyMsg.addressForRead(0), 0,
            secretKey.addressForRead(0)));

    sigAndMsg.readPositionRemaining(0, 64);

    System.out.println(publicKey.toHexString());
    System.out.println(privateKey.toHexString());
    System.out.println(secretKey.toHexString());
    assertEquals(sigAndMsg.toHexString(), sigAndMsg0.toHexString());
    checkZeros(emptyMsg);
    checkZeros(secretKey);
    checkZeros(sigAndMsg);

    Bytes buffer = bytesWithZeros(ED25519_SECRETKEY_BYTES);

    assertTrue(Ed25519.verify(sigAndMsg, publicKey));
    for (int i = 0; i < sigAndMsg.readRemaining(); i++) {
        byte old = sigAndMsg.readByte(i);
        sigAndMsg.writeByte(i, old ^ 1);
        assertFalse(Ed25519.verify(sigAndMsg, publicKey));
        sigAndMsg.writeByte(i, old);
    }

    assertEquals(0, SODIUM.crypto_sign_ed25519_open(buffer.addressForWrite(0), new LongLongByReference(0), sigAndMsg.addressForRead(0), 0 + 64,
            publicKey.addressForRead(0)));
    sigAndMsg.readPositionRemaining(0, 64);
    System.out.println(sigAndMsg.toHexString());

}