Search in sources :

Example 1 with Ed25519Sign

use of com.google.crypto.tink.subtle.Ed25519Sign in project tink by google.

the class Ed25519PrivateKeyManagerTest method testBasic.

@Test
public void testBasic() throws Exception {
    Ed25519PrivateKeyManager manager = new Ed25519PrivateKeyManager();
    KeyTemplate template = SignatureKeyTemplates.ED25519;
    MessageLite key = manager.newKey(template);
    assertTrue(key instanceof Ed25519PrivateKey);
    Ed25519PrivateKey keyProto = (Ed25519PrivateKey) key;
    assertEquals(32, keyProto.getKeyValue().size());
    PublicKeySign signer = manager.getPrimitive(key);
    assertTrue(signer instanceof Ed25519Sign);
    byte[] message = Random.randBytes(20);
    byte[] signature = signer.sign(message);
    assertEquals(64, signature.length);
    Ed25519PublicKeyManager publicKeyManager = new Ed25519PublicKeyManager();
    PublicKeyVerify verifier = publicKeyManager.getPrimitive(keyProto.getPublicKey());
    assertTrue(verifier instanceof Ed25519Verify);
    try {
        verifier.verify(signature, message);
    } catch (GeneralSecurityException e) {
        fail("Do not expect GeneralSecurityException: " + e);
    }
}
Also used : Ed25519PrivateKey(com.google.crypto.tink.proto.Ed25519PrivateKey) Ed25519Verify(com.google.crypto.tink.subtle.Ed25519Verify) GeneralSecurityException(java.security.GeneralSecurityException) PublicKeyVerify(com.google.crypto.tink.PublicKeyVerify) Ed25519Sign(com.google.crypto.tink.subtle.Ed25519Sign) KeyTemplate(com.google.crypto.tink.proto.KeyTemplate) MessageLite(com.google.protobuf.MessageLite) PublicKeySign(com.google.crypto.tink.PublicKeySign) Test(org.junit.Test)

Example 2 with Ed25519Sign

use of com.google.crypto.tink.subtle.Ed25519Sign in project tink by google.

the class Ed25519PrivateKeyManager method getPrimitive.

@Override
public PublicKeySign getPrimitive(MessageLite key) throws GeneralSecurityException {
    if (!(key instanceof Ed25519PrivateKey)) {
        throw new GeneralSecurityException("expected Ed25519PrivateKey proto");
    }
    Ed25519PrivateKey keyProto = (Ed25519PrivateKey) key;
    validate(keyProto);
    return new Ed25519Sign(keyProto.getKeyValue().toByteArray());
}
Also used : Ed25519PrivateKey(com.google.crypto.tink.proto.Ed25519PrivateKey) GeneralSecurityException(java.security.GeneralSecurityException) Ed25519Sign(com.google.crypto.tink.subtle.Ed25519Sign)

Aggregations

Ed25519PrivateKey (com.google.crypto.tink.proto.Ed25519PrivateKey)2 Ed25519Sign (com.google.crypto.tink.subtle.Ed25519Sign)2 GeneralSecurityException (java.security.GeneralSecurityException)2 PublicKeySign (com.google.crypto.tink.PublicKeySign)1 PublicKeyVerify (com.google.crypto.tink.PublicKeyVerify)1 KeyTemplate (com.google.crypto.tink.proto.KeyTemplate)1 Ed25519Verify (com.google.crypto.tink.subtle.Ed25519Verify)1 MessageLite (com.google.protobuf.MessageLite)1 Test (org.junit.Test)1