use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class CleartextKeysetHandleTest method testRead.
@Test
public void testRead() throws Exception {
// Create a keyset that contains a single HmacKey.
KeyTemplate template = MacKeyTemplates.HMAC_SHA256_128BITTAG;
KeysetManager manager = KeysetManager.withEmptyKeyset().rotate(template);
Keyset keyset1 = manager.getKeysetHandle().getKeyset();
KeysetHandle handle1 = CleartextKeysetHandle.read(BinaryKeysetReader.withBytes(keyset1.toByteArray()));
assertEquals(keyset1, handle1.getKeyset());
KeysetHandle handle2 = KeysetHandle.generateNew(template);
Keyset keyset2 = handle2.getKeyset();
assertEquals(1, keyset2.getKeyCount());
Keyset.Key key2 = keyset2.getKey(0);
assertEquals(keyset2.getPrimaryKeyId(), key2.getKeyId());
assertEquals(template.getTypeUrl(), key2.getKeyData().getTypeUrl());
Mac unused = handle2.getPrimitive(Mac.class);
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class CleartextKeysetHandleTest method testParse.
@Test
public void testParse() throws Exception {
// Create a keyset that contains a single HmacKey.
KeyTemplate template = MacKeyTemplates.HMAC_SHA256_128BITTAG;
KeysetHandle handle = KeysetHandle.generateNew(template);
Keyset keyset = CleartextKeysetHandle.getKeyset(handle);
handle = CleartextKeysetHandle.parseFrom(keyset.toByteArray());
assertEquals(keyset, handle.getKeyset());
handle.getPrimitive(Mac.class);
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class JsonKeysetWriterTest method testWrite_writesNegativeIdAsPositive.
@Test
public void testWrite_writesNegativeIdAsPositive() throws Exception {
int magicKeyId = -19230912;
Keyset unmodified = CleartextKeysetHandle.getKeyset(KeysetHandle.generateNew(MacKeyTemplates.HMAC_SHA256_128BITTAG));
Keyset modified = Keyset.newBuilder(unmodified).setPrimaryKeyId(magicKeyId).setKey(0, Keyset.Key.newBuilder(unmodified.getKey(0)).setKeyId(magicKeyId).build()).build();
KeysetHandle modifiedHandle = CleartextKeysetHandle.parseFrom(modified.toByteArray());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
CleartextKeysetHandle.write(modifiedHandle, JsonKeysetWriter.withOutputStream(outputStream));
assertThat(outputStream.toString()).contains("\"primaryKeyId\":4275736384");
assertThat(outputStream.toString()).contains("\"keyId\":4275736384");
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class KeysetHandleTest method getKeys.
@Test
public void getKeys() throws Exception {
KeyTemplate keyTemplate = KeyTemplates.get("AES128_EAX");
KeysetManager keysetManager = KeysetManager.withEmptyKeyset();
final int numKeys = 3;
for (int i = 0; i < numKeys; i++) {
keysetManager.add(keyTemplate);
}
KeysetHandle handle = keysetManager.getKeysetHandle();
Keyset keyset = handle.getKeyset();
List<KeyHandle> keysetKeys = handle.getKeys();
expect.that(keysetKeys).hasSize(numKeys);
Map<Integer, KeyHandle> keysetKeysMap = keysetKeys.stream().collect(Collectors.toMap(KeyHandle::getId, key -> key));
for (Keyset.Key key : keyset.getKeyList()) {
expect.that(keysetKeysMap).containsKey(key.getKeyId());
KeyHandle keysetKey = keysetKeysMap.get(key.getKeyId());
expect.that(KeyStatusTypeProtoConverter.toProto(keysetKey.getStatus())).isEqualTo(key.getStatus());
KeyData keyData = ((ProtoKey) keysetKey.getKey(SecretKeyAccess.insecureSecretAccess())).getProtoKey();
expect.that(keyData).isEqualTo(key.getKeyData());
}
}
use of com.google.crypto.tink.proto.Keyset in project tink by google.
the class TinkProtoTest method testKeysetBasic.
@Test
public void testKeysetBasic() throws Exception {
Keyset keyset = Keyset.newBuilder().setPrimaryKeyId(1).build();
assertEquals(1, keyset.getPrimaryKeyId());
}
Aggregations