I have two PublicKey object.I want to compare both for equality or to check which is latest object using java security API or bouncy castle API.How can i achieve this?
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
Normally public keys are compared using some kind of ID. It depends on the protocol how the key ID is calculated. The best method is probably to keep to the PKCS#11 specifications which defines methods of calculating key ID's.
The creation date is not an integral part of the key itself. Either you have to define it elsewhere, or you should use a public key container such as an X509 certificate. Note that you could use the (hex representation of the) key ID to find the creation date in a map.
It's probably best to use a SHA-1 hash over the modulus as ID. The modulus of both the public key and private key are identical and should be different for each key pair. The following code calculates the ID for an RSA public key.
Obviously you can always directly compare the moduli of two keys as well. Key ID's are a bit easier to store though.
Note that the
getModulus()
command may not be compatible with some keystores (e.g. those ones that represent HSM tokens or smart cards).You can use
equals
or check the hashcode of the keys
or compare the hex string of the two public keys
You have a lot of key comparision and check samples at Bouncy Castle Tests, take a look at the
org.bouncycastle.jce.provider.test
package for some code. BC is not strictly necesary you can do the comparision with the default java security classes.Lookin at Oracle's documentation, I think you can compare
PublicKey
using its 3 getters :getAlgorithm
,getEncoded
,getFormat
doing this :oldKey.getAlgorithm().equals(newKey.getAlgorithm())
and so on.