6

私は2つのPublicKeyオブジェクトを持っています.JavaセキュリティAPIまたはバウンシーキャッスルAPIを使用して、両方を比較したり、どちらが最新のオブジェクトであるかを確認したりしたいのですが、どうすればこれを達成できますか?

4

3 に答える 3

6

You can use equals

if (!key.equals(copyKey)){
    System.out.println("not equals!");
}

or check the hashcode of the keys

if (key.hashCode() != copyKey.hashCode())
{
    System.out.println("public key hashCode check failed");
}

or compare the hex string of the two public keys

String encodedKey1 = new String(Hex.encode(key1.getEncoded()));
String encodedKey2 = new String(Hex.encode(key2.getEncoded()));

if (!encodedKey1.equals(encodedKey2)){
    System.out.println("not equals!");
}

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.

于 2014-01-20T17:52:34.840 に答える
0

OracleのドキュメントPublicKeyを見ると、3つのゲッターを使用して比較できると思います: getAlgorithm、、これgetEncodedgetFormat行う:

oldKey.getAlgorithm().equals(newKey.getAlgorithm())等々。

于 2014-01-20T10:20:27.367 に答える