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

The following examples show how to use net.openhft.chronicle.bytes.Bytes#allocateDirect() . 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: Ed25519Test.java    From Chronicle-Salt with Apache License 2.0 6 votes vote down vote up
@Test
public void sign2() {
    final String SIGN_PRIVATE = "B18E1D0045995EC3D010C387CCFEB984D783AF8FBB0F40FA7DB126D889F6DADD";
    Bytes privateKey = fromHex(SIGN_PRIVATE);
    Bytes publicKey = bytesWithZeros(32);
    Bytes secretKey = bytesWithZeros(64);
    Ed25519.privateToPublicAndSecret(publicKey, secretKey, privateKey);
    checkZeros(secretKey);
    checkZeros(privateKey);

    Bytes message = privateKey;
    Bytes signAndMsg = Bytes.allocateDirect(Ed25519.SIGNATURE_LENGTH + message.readRemaining());
    signAndMsg.writeSkip(Ed25519.SIGNATURE_LENGTH);
    signAndMsg.write(privateKey);
    Ed25519.sign(signAndMsg, secretKey);

    String SIGN_EXPECTED = "86b4707fadb1ef4613efadd12143cd9dffb2eac329c38923c03f9e315c3dd33bde1ef101137fbc403eb3f3d7ff283155053c667eb65908fe6fcd653eab550e0f";
    Bytes signExpected = fromHex(SIGN_EXPECTED + SIGN_PRIVATE);
    assertEquals(signExpected.toHexString(), signAndMsg.toHexString());

    signAndMsg.releaseLast();
}
 
Example 2
Source File: Ed25519Test.java    From Chronicle-Salt with Apache License 2.0 6 votes vote down vote up
@Test
public void sign3() {
    final String SIGN_PRIVATE = "B18E1D0045995EC3D010C387CCFEB984D783AF8FBB0F40FA7DB126D889F6DADD";
    Bytes privateKey = fromHex(SIGN_PRIVATE);
    Bytes publicKey = bytesWithZeros(32);
    Bytes secretKey = bytesWithZeros(64);
    Ed25519.privateToPublicAndSecret(publicKey, secretKey, privateKey);
    checkZeros(secretKey);
    checkZeros(privateKey);

    Bytes message = privateKey;
    Bytes signAndMsg = Bytes.allocateDirect(Ed25519.SIGNATURE_LENGTH + message.readRemaining());
    Ed25519.sign(signAndMsg, message, secretKey);

    String SIGN_EXPECTED = "86b4707fadb1ef4613efadd12143cd9dffb2eac329c38923c03f9e315c3dd33bde1ef101137fbc403eb3f3d7ff283155053c667eb65908fe6fcd653eab550e0f";
    Bytes signExpected = fromHex(SIGN_EXPECTED + SIGN_PRIVATE);
    assertEquals(signExpected.toHexString(), signAndMsg.toHexString());

    signAndMsg.releaseLast();
}
 
Example 3
Source File: BatchSha256Rc4Test.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
public static Bytes<?> generateRc4(long len) {
    int[] key = new int[] { 0 };
    Rc4Cipher cipher = new Rc4Cipher(key);
    Bytes<?> bytes = Bytes.allocateDirect(len);
    cipher.prga(bytes, len);
    return bytes;
}
 
Example 4
Source File: Ed25519.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
public KeyPair(char ch) {
    Bytes<Void> privateKey = Bytes.allocateDirect(PRIVATE_KEY_LENGTH);
    while (privateKey.writeRemaining() > 0)
        privateKey.append(ch);
    privateToPublicAndSecret(publicKey, secretKey, privateKey);
    privateKey.releaseLast();
}
 
Example 5
Source File: Sodium.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
static Bytes<?> fromHex(int padding, String s) {
    byte[] byteArr = DatatypeConverter.parseHexBinary(s);
    Bytes<?> bytes = Bytes.allocateDirect(padding + byteArr.length);
    if (padding > 0) {
        bytes.zeroOut(0, padding);
        bytes.writePosition(padding);
    }
    bytes.write(byteArr);
    return bytes;
}
 
Example 6
Source File: Signature.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
/**
 * Initialise a wrapper for a single multi-part message exchange
 */
public MultiPart() {
    this.state = Bytes.allocateDirect(SIZEOF_CRYPTO_SIGN_STATE);
    ((Bytes) state).readLimit(SIZEOF_CRYPTO_SIGN_STATE);

    SODIUM.crypto_sign_init(state.addressForRead(0));
}
 
Example 7
Source File: SHA2.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
/**
 * Initialise a wrapper for a single multi-part message exchange
 */
public MultiPartSHA512() {
    this.state = Bytes.allocateDirect(SIZEOF_CRYPTO_HASH_SHA512_STATE);
    ((Bytes) state).readLimit(SIZEOF_CRYPTO_HASH_SHA512_STATE);

    Sodium.SODIUM.crypto_hash_sha512_init(state.addressForRead(0));
}
 
Example 8
Source File: SHA2.java    From Chronicle-Salt with Apache License 2.0 5 votes vote down vote up
/**
 * Initialise a wrapper for a single multi-part message exchange
 */
public MultiPartSHA256() {
    this.state = Bytes.allocateDirect(SIZEOF_CRYPTO_HASH_SHA256_STATE);
    ((Bytes) state).readLimit(SIZEOF_CRYPTO_HASH_SHA256_STATE);

    Sodium.SODIUM.crypto_hash_sha256_init(state.addressForRead(0));
}
 
Example 9
Source File: SealedBox.java    From Chronicle-Salt with Apache License 2.0 4 votes vote down vote up
private SecretKey() {
    this.store = Bytes.allocateDirect(CRYPTO_BOX_SECRETKEYBYTES);
    ((Bytes) store).readLimit(CRYPTO_BOX_SECRETKEYBYTES);
}
 
Example 10
Source File: GeneratePublicKeyPerfMain.java    From Chronicle-Salt with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    Bytes<Void> privateKey0 = Bytes.allocateDirect(Ed25519.PRIVATE_KEY_LENGTH);
    Ed25519.generatePrivateKey(privateKey0);

    int nThreads = Runtime.getRuntime().availableProcessors();
    ExecutorService es = Executors.newFixedThreadPool(nThreads);
    long[] min = { 1L << 32 };
    for (int i = 0; i < nThreads; i++) {
        es.execute(() -> {
            try {
                Bytes<Void> publicKey = Ed25519.allocatePublicKey();
                Bytes<Void> secretKey = Ed25519.allocateSecretKey();
                int j = 0;
                long start = System.nanoTime();
                Bytes<Void> privateKey = Bytes.allocateDirect(Ed25519.PRIVATE_KEY_LENGTH);
                OUTER: do {
                    if (j++ == 0)
                        Ed25519.generatePrivateKey(privateKey);
                    else if (j > 32)
                        j = 0;
                    publicKey.clear();
                    secretKey.clear();
                    privateKey.addAndGetLong(0, 1);
                    Ed25519.privateToPublicAndSecret(publicKey, secretKey, privateKey);
                    for (int k = 0; k <= Ed25519.PUBLIC_KEY_LENGTH - 4; k++) {
                        long value = publicKey.readUnsignedInt(k);
                        if (value == 0) {
                            System.out.println("k= " + k);
                            break OUTER;
                        }
                    }
                } while (true);
                System.out.println(privateKey.toHexString());
                System.out.println(publicKey.toHexString());
                long time = System.nanoTime() - start;
                System.out.printf("Took %.3f seconds%n", time / 1e9);
            } catch (Throwable t) {
                t.printStackTrace();
            }
        });
    }
    es.shutdownNow();
}
 
Example 11
Source File: SignAndVerifyPerfMain.java    From Chronicle-Salt with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    final String SIGN_PRIVATE = "b18e1d0045995ec3d010c387ccfeb984d783af8fbb0f40fa7db126d889f6dadd";

    Bytes<?> publicKey = Bytes.allocateDirect(32);
    Bytes<?> secretKey = Bytes.allocateDirect(64);
    BytesStore<?, ?> privateKey = fromHex(SIGN_PRIVATE);
    Ed25519.privateToPublicAndSecret(publicKey, secretKey, privateKey);
    assertEquals(32, publicKey.readRemaining());
    assertEquals(64, secretKey.readRemaining());

    ThreadLocal<Bytes<?>> sigAndMsg = ThreadLocal.withInitial(() -> Bytes.allocateDirect(64 + 64));
    ThreadLocal<Bytes<?>> sigAndMsg2 = ThreadLocal.withInitial(() -> Bytes.allocateDirect(64 + 64));

    int procs = Runtime.getRuntime().availableProcessors();
    for (int t = 0; t < 10; t++) {
        int runs = procs * 20;
        long start = System.nanoTime();
        IntStream.range(0, runs).parallel().forEach(i -> {
            sigAndMsg.get().writePosition(0);
            Ed25519.sign(sigAndMsg.get(), secretKey, secretKey);
            sigAndMsg2.get().writePosition(0);
            Ed25519.sign(sigAndMsg2.get(), privateKey, secretKey);
        });
        long time = System.nanoTime() - start;

        Bytes<?> bytes = sigAndMsg.get();
        bytes.writePosition(0);
        Bytes<?> bytes2 = sigAndMsg2.get();
        bytes2.writePosition(0);
        Ed25519.sign(bytes, secretKey, secretKey);
        Ed25519.sign(bytes2, privateKey, secretKey);
        long start2 = System.nanoTime();
        IntStream.range(0, runs).parallel().forEach(i -> {
            assertTrue(Ed25519.verify(bytes, publicKey));
            assertTrue(Ed25519.verify(bytes2, publicKey));
        });
        long time2 = System.nanoTime() - start2;
        System.out.println(
                "Throughput: " + "Sign: " + (long) ((2 * runs * 1e9) / time) + "/s, " + "Verify: " + (long) ((2 * runs * 1e9) / time2) + "/s");
        Jvm.pause(100);
    }
}
 
Example 12
Source File: SignAndVerifyPerfMain.java    From Chronicle-Salt with Apache License 2.0 4 votes vote down vote up
static Bytes<?> fromHex(String s) {
    byte[] byteArr = DatatypeConverter.parseHexBinary(s);
    Bytes<?> bytes = Bytes.allocateDirect(byteArr.length);
    bytes.write(byteArr);
    return bytes;
}
 
Example 13
Source File: SealedBox.java    From Chronicle-Salt with Apache License 2.0 4 votes vote down vote up
private PublicKey() {
    this.store = Bytes.allocateDirect(CRYPTO_BOX_PUBLICKEYBYTES);
    ((Bytes) store).readLimit(CRYPTO_BOX_PUBLICKEYBYTES);
}
 
Example 14
Source File: Signature.java    From Chronicle-Salt with Apache License 2.0 4 votes vote down vote up
private SecretKey() {
    this.store = Bytes.allocateDirect(CRYPTO_SIGN_SECRETKEYBYTES);
    ((Bytes) store).readLimit(CRYPTO_SIGN_SECRETKEYBYTES);
}
 
Example 15
Source File: Signature.java    From Chronicle-Salt with Apache License 2.0 4 votes vote down vote up
private PublicKey() {
    this.store = Bytes.allocateDirect(CRYPTO_SIGN_PUBLICKEYBYTES);
    ((Bytes) store).readLimit(CRYPTO_SIGN_PUBLICKEYBYTES);
}
 
Example 16
Source File: EasyBox.java    From Chronicle-Salt with Apache License 2.0 4 votes vote down vote up
private SharedKey() {
    this.store = Bytes.allocateDirect(CRYPTO_BOX_BEFORENMBYTES);
    ((Bytes) store).readLimit(CRYPTO_BOX_BEFORENMBYTES);
}
 
Example 17
Source File: EasyBox.java    From Chronicle-Salt with Apache License 2.0 4 votes vote down vote up
private SecretKey() {
    this.store = Bytes.allocateDirect(CRYPTO_BOX_SECRETKEYBYTES);
    ((Bytes) store).readLimit(CRYPTO_BOX_SECRETKEYBYTES);
}
 
Example 18
Source File: BytesForTesting.java    From Chronicle-Salt with Apache License 2.0 4 votes vote down vote up
Bytes bytesWithZeros(long size) {
    Bytes b = Bytes.allocateDirect(size + 32);
    b.zeroOut(0, b.realCapacity());
    bytesList.add(b);
    return b;
}
 
Example 19
Source File: Signature.java    From Chronicle-Salt with Apache License 2.0 2 votes vote down vote up
/**
 * Generate deterministic public/private key pair from simple long id (which only uses 8 out of 32 seed bytes)
 *
 * @param id
 *            - deterministic seed
 */
public static KeyPair deterministic(long id) {
    BytesStore seed = Bytes.allocateDirect(CRYPTO_SIGN_SEEDBYTES);
    seed.writeLong(0, id);
    return deterministic(seed);
}
 
Example 20
Source File: EasyBox.java    From Chronicle-Salt with Apache License 2.0 2 votes vote down vote up
/**
 * Generate deterministic public/private key pair from simple long id (which only uses 8 out of 32 seed bytes)
 *
 * @param id
 *            - deterministic seed
 */
public static KeyPair deterministic(long id) {
    BytesStore seed = Bytes.allocateDirect(CRYPTO_BOX_SEEDBYTES);
    seed.writeLong(0, id);
    return deterministic(seed);
}