Java Code Examples for org.apache.tuweni.bytes.Bytes#random()

The following examples show how to use org.apache.tuweni.bytes.Bytes#random() . 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: BlockHeaderTest.java    From incubator-tuweni with Apache License 2.0 6 votes vote down vote up
static BlockHeader generateBlockHeader() {
  return new BlockHeader(
      Hash.fromBytes(Bytes.random(32)),
      Hash.fromBytes(Bytes.random(32)),
      Address.fromBytes(Bytes.fromHexString("0x0102030405060708091011121314151617181920")),
      Hash.fromBytes(Bytes.random(32)),
      Hash.fromBytes(Bytes.random(32)),
      Hash.fromBytes(Bytes.random(32)),
      Bytes.random(8),
      UInt256.fromBytes(Bytes.random(32)),
      UInt256.fromBytes(Bytes.random(32)),
      Gas.valueOf(UInt256.fromBytes(Bytes.random(6))),
      Gas.valueOf(UInt256.fromBytes(Bytes.random(6))),
      Instant.now().truncatedTo(ChronoUnit.SECONDS),
      Bytes.random(22),
      Hash.fromBytes(Bytes.random(32)),
      UInt64.ONE);
}
 
Example 2
Source File: SecureScuttlebuttStreamTest.java    From incubator-tuweni with Apache License 2.0 6 votes vote down vote up
@Test
void streamExchange() {
  SHA256Hash.Hash clientToServerKey = SHA256Hash.hash(SHA256Hash.Input.fromBytes(Bytes32.random()));
  Bytes clientToServerNonce = Bytes.random(24);
  SHA256Hash.Hash serverToClientKey = SHA256Hash.hash(SHA256Hash.Input.fromBytes(Bytes32.random()));
  Bytes serverToClientNonce = Bytes.random(24);
  SecureScuttlebuttStream clientToServer =
      new SecureScuttlebuttStream(clientToServerKey, clientToServerNonce, serverToClientKey, serverToClientNonce);
  SecureScuttlebuttStream serverToClient =
      new SecureScuttlebuttStream(clientToServerKey, clientToServerNonce, serverToClientKey, serverToClientNonce);

  Bytes encrypted = clientToServer.sendToServer(Bytes.fromHexString("deadbeef"));
  assertEquals(Bytes.fromHexString("deadbeef").size() + 34, encrypted.size());

  Bytes decrypted = serverToClient.readFromClient(encrypted);
  assertEquals(Bytes.fromHexString("deadbeef"), decrypted);

  Bytes response = serverToClient.sendToClient(Bytes.fromHexString("deadbeef"));
  assertEquals(Bytes.fromHexString("deadbeef").size() + 34, response.size());

  Bytes responseDecrypted = clientToServer.readFromServer(response);
  assertEquals(Bytes.fromHexString("deadbeef"), responseDecrypted);
}
 
Example 3
Source File: SecureScuttlebuttStreamTest.java    From incubator-tuweni with Apache License 2.0 6 votes vote down vote up
@Test
void longMessage() {
  SHA256Hash.Hash clientToServerKey = SHA256Hash.hash(SHA256Hash.Input.fromBytes(Bytes32.random()));
  Bytes clientToServerNonce = Bytes.random(24);
  SHA256Hash.Hash serverToClientKey = SHA256Hash.hash(SHA256Hash.Input.fromBytes(Bytes32.random()));
  Bytes serverToClientNonce = Bytes.random(24);
  SecureScuttlebuttStream clientToServer =
      new SecureScuttlebuttStream(clientToServerKey, clientToServerNonce, serverToClientKey, serverToClientNonce);
  SecureScuttlebuttStream serverToClient =
      new SecureScuttlebuttStream(clientToServerKey, clientToServerNonce, serverToClientKey, serverToClientNonce);

  Bytes payload = Bytes.random(5128);
  Bytes encrypted = clientToServer.sendToServer(payload);
  assertEquals(5128 + 34 + 34, encrypted.size());
  Bytes decrypted = serverToClient.readFromClient(encrypted);
  assertEquals(payload, decrypted);

  Bytes encrypted2 = serverToClient.sendToClient(payload);
  assertEquals(5128 + 34 + 34, encrypted2.size());

  Bytes decrypted2 = clientToServer.readFromServer(encrypted2);
  assertEquals(payload, decrypted2);
}
 
Example 4
Source File: SecureScuttlebuttStreamTest.java    From incubator-tuweni with Apache License 2.0 6 votes vote down vote up
@Test
void multipleMessages() {
  SHA256Hash.Hash clientToServerKey = SHA256Hash.hash(SHA256Hash.Input.fromBytes(Bytes32.random()));
  Bytes clientToServerNonce = Bytes.random(24);
  SHA256Hash.Hash serverToClientKey = SHA256Hash.hash(SHA256Hash.Input.fromBytes(Bytes32.random()));
  Bytes serverToClientNonce = Bytes.random(24);
  SecureScuttlebuttStream clientToServer =
      new SecureScuttlebuttStream(clientToServerKey, clientToServerNonce, serverToClientKey, serverToClientNonce);
  SecureScuttlebuttStream serverToClient =
      new SecureScuttlebuttStream(clientToServerKey, clientToServerNonce, serverToClientKey, serverToClientNonce);

  for (int i = 0; i < 10; i++) {
    Bytes encrypted = clientToServer.sendToServer(Bytes.fromHexString("deadbeef"));
    assertEquals(Bytes.fromHexString("deadbeef").size() + 34, encrypted.size());

    Bytes decrypted = serverToClient.readFromClient(encrypted);
    assertEquals(Bytes.fromHexString("deadbeef"), decrypted);

    Bytes response = serverToClient.sendToClient(Bytes.fromHexString("deadbeef"));
    assertEquals(Bytes.fromHexString("deadbeef").size() + 34, response.size());

    Bytes responseDecrypted = clientToServer.readFromServer(response);
    assertEquals(Bytes.fromHexString("deadbeef"), responseDecrypted);
  }
}
 
Example 5
Source File: SecureScuttlebuttStreamTest.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
@Test
void chunkedMessages() {
  SHA256Hash.Hash clientToServerKey = SHA256Hash.hash(SHA256Hash.Input.fromBytes(Bytes32.random()));
  Bytes clientToServerNonce = Bytes.random(24);
  SHA256Hash.Hash serverToClientKey = SHA256Hash.hash(SHA256Hash.Input.fromBytes(Bytes32.random()));
  Bytes serverToClientNonce = Bytes.random(24);
  SecureScuttlebuttStream clientToServer =
      new SecureScuttlebuttStream(clientToServerKey, clientToServerNonce, serverToClientKey, serverToClientNonce);
  SecureScuttlebuttStream serverToClient =
      new SecureScuttlebuttStream(clientToServerKey, clientToServerNonce, serverToClientKey, serverToClientNonce);

  Bytes payload = Bytes.random(5128);
  Bytes encrypted = clientToServer.sendToServer(payload);
  assertEquals(5128 + 34 + 34, encrypted.size());
  serverToClient.readFromClient(encrypted.slice(0, 20));
  serverToClient.readFromClient(encrypted.slice(20, 400));
  Bytes part1 = serverToClient.readFromClient(encrypted.slice(420, 4000));
  Bytes decrypted = serverToClient.readFromClient(encrypted.slice(4420));
  assertEquals(payload, Bytes.concatenate(part1, decrypted));

  Bytes encrypted2 = serverToClient.sendToClient(payload);
  assertEquals(5128 + 34 + 34, encrypted2.size());
  clientToServer.readFromServer(encrypted2.slice(0, 20));
  clientToServer.readFromServer(encrypted2.slice(20, 400));
  Bytes part2 = clientToServer.readFromServer(encrypted2.slice(420, 4000));
  Bytes decrypted2 = clientToServer.readFromServer(encrypted2.slice(4420));
  assertEquals(payload, Bytes.concatenate(part2, decrypted2));
}
 
Example 6
Source File: SecureScuttlebuttStreamTest.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
@Test
void sendingGoodbyes() {
  SHA256Hash.Hash clientToServerKey = SHA256Hash.hash(SHA256Hash.Input.fromBytes(Bytes32.random()));
  Bytes clientToServerNonce = Bytes.random(24);
  SHA256Hash.Hash serverToClientKey = SHA256Hash.hash(SHA256Hash.Input.fromBytes(Bytes32.random()));
  Bytes serverToClientNonce = Bytes.random(24);
  SecureScuttlebuttStream clientToServer =
      new SecureScuttlebuttStream(clientToServerKey, clientToServerNonce, serverToClientKey, serverToClientNonce);
  SecureScuttlebuttStream serverToClient =
      new SecureScuttlebuttStream(clientToServerKey, clientToServerNonce, serverToClientKey, serverToClientNonce);
  Bytes message = clientToServer.sendGoodbyeToServer();
  assertTrue(SecureScuttlebuttStreamServer.isGoodbye(serverToClient.readFromClient(message)));
  assertTrue(
      SecureScuttlebuttStreamServer.isGoodbye(clientToServer.readFromServer(serverToClient.sendGoodbyeToClient())));
}
 
Example 7
Source File: ConcatenateTest.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
@Test
void testConcatenateTwoValues() {
  Concatenate concatenate = new Concatenate();
  Bytes random = Bytes.random(32);

  concatenate.add(Signature.PublicKey.fromBytes(random));
  concatenate.add(Signature.PublicKey.fromBytes(random));

  Allocated result = concatenate.concatenate();

  assertEquals(Bytes.concatenate(random, random), result.bytes());
}
 
Example 8
Source File: SingleQueryParameterUtilsTest.java    From teku with Apache License 2.0 5 votes vote down vote up
@Test
public void getParameterAsBLSSignature_shouldParseBytes96Data() {
  BLSSignature signature = new BLSSignature(Bytes.random(96));
  Map<String, List<String>> data = Map.of(KEY, List.of(signature.toHexString()));
  BLSSignature result = getParameterValueAsBLSSignature(data, KEY);
  assertEquals(signature, result);
}
 
Example 9
Source File: EncryptedKeystoreWriter.java    From teku with Apache License 2.0 5 votes vote down vote up
private KeyStoreData generateKeystoreData(final BLSKeyPair key, final String password) {
  final KdfParam kdfParam = new SCryptParam(32, Bytes32.random(secureRandom));
  final Cipher cipher = new Cipher(CipherFunction.AES_128_CTR, Bytes.random(16, secureRandom));
  return KeyStore.encrypt(
      key.getSecretKey().toBytes(),
      key.getPublicKey().toBytesCompressed(),
      password,
      "",
      kdfParam,
      cipher);
}
 
Example 10
Source File: HashTreeRootTest.java    From incubator-tuweni with Apache License 2.0 4 votes vote down vote up
@Test
void hashBytes34() {
  Bytes someBytes = Bytes.random(34);
  assertEquals(Hash.keccak256(someBytes), SSZ.hashTreeRoot(someBytes));
}
 
Example 11
Source File: BenchBLS.java    From teku with Apache License 2.0 4 votes vote down vote up
@Setup
public void setup() {
  message = Bytes.random(32);
}