我们希望在我们的项目中使用丁克库来验证一些给定公钥的传入签名。
我们的情况如下:
在阅读了丁克的文档之后,我们无法理解如何加载公钥字符串,以便PublicKeyVerifyFactory可以使用它。
有人做过类似的事吗?你有没有在网上找到任何能指引我们走向正确方向的例子?
发布于 2019-09-11 13:53:48
您可以通过KeysetHandle从公钥创建CleartextKeysetHandle.read(),然后获取它的原语,然后验证签名。您不需要知道KeysetHandle的私有部分就可以做到这一点,这就是首先使用非对称密钥的意义所在。
问题是,为了以后使用这个read(),我应该导出什么?有几种方法,但一种方法是将PublicKeysetHandle导出为JSON格式。您可以使用CleartextKeysetHandle.write()和JsonKeysetWriter.withOutputStream()导出它,然后可以使用CleartextKeysetHandle.read()和JsonKeysetReader.withBytes()将其转换回KeysetHandle。
所以,你是鲍勃,想公开你对爱丽丝的公开密钥。在您的服务中,您将生成私钥,提取公钥,将其转换为JSON格式,并以某种方式导出它,比如REST端点:
Bob的应用程序
SignatureConfig.register();
// This is your, and only yours, private key.
KeysetHandle privateKeysetHandle =
KeysetHandle.generateNew(SignatureKeyTemplates.ECDSA_P256);
// This is the public key extracted from the private key.
KeysetHandle publicKeysetHandle = privateKeysetHandle.getPublicKeysetHandle();
ByteArrayOutputStream publicKeyStream = new ByteArrayOutputStream();
CleartextKeysetHandle.write(publicKeysetHandle,
JsonKeysetWriter.withOutputStream(publicKeyStream));
// And this is the public key in JSON format.
// You can publish this in a REST endpoint.
return publicKeyStream.toString();Alice的应用程序
String publicKey = getThatJsonPublicKeyFromBobsEndpoint();
// Here the JSON with only the public key is converted into a KeysetHandle.
KeysetHandle keysetHandle = CleartextKeysetHandle
.read(JsonKeysetReader.withBytes(publicKey.getBytes()));
// Getting the signature verifier from the public keyset handler.
PublicKeyVerify verifier = keysetHandle.getPrimitive(PublicKeyVerify.class);
// And finally verify Bob's signature for his message.
verifier.verify(bobsMessage.getSignature(), bobsMessage.getData());在Bob的应用程序中,每次都生成私钥。您可能希望保持相同的私钥,因此您需要存储该私钥并恢复它,就像爱丽丝的应用程序一样,但是如果使用PublicKeysetHandle,则需要使用PrivateKeysetHandle。上面的示例只是演示如何将公钥导出为字符串格式,并在以后的其他应用程序中恢复它。
发布于 2019-01-17 23:04:21
丁克在protobuf中存储公钥。有一天,我将编写一些允许将公共密钥格式(如PEM或JWK )转换为protobuf的代码,但在此之前,我担心您必须自己编写代码(并做出贡献!)。
发布于 2019-06-21 18:30:26
下面是一些代码示例片段来说明:
public static boolean verify(byte[] data, byte[] signature, KeysetHandle publicKeysetHandle, CIPHER_ASYMMETRIC_ALGOS algo_chosen) throws IOException, GeneralSecurityException {
TinkConfig.register();
boolean status_verification = False;
try {
PublicKeyVerify verifier = PublicKeyVerifyFactory.getPrimitive( publicKeysetHandle);
verifier.verify(signature, data);
status_verification = True;
} catch (GeneralSecurityException e) {
status_verification = False;
}
return status_verification;
}//假设您已经有了以字节为单位的签名。
用法:
boolean status_verification = verify(data, signature, publicKeysetHandle);
if(status_verification == True){
System.out.println(“status_verification: PASS”);
} else {
System.out.println(“status_verification: FAIL”);
}https://stackoverflow.com/questions/53228475
复制相似问题