blob: 9b9b7f26edb1b270eb60cc2052f9b3b01c13a289 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
package org.spongycastle.openpgp.operator.bc;
import java.io.IOException;
import org.spongycastle.bcpg.BCPGKey;
import org.spongycastle.bcpg.MPInteger;
import org.spongycastle.bcpg.PublicKeyPacket;
import org.spongycastle.bcpg.RSAPublicBCPGKey;
import org.spongycastle.crypto.Digest;
import org.spongycastle.crypto.digests.MD5Digest;
import org.spongycastle.crypto.digests.SHA1Digest;
import org.spongycastle.openpgp.PGPException;
import org.spongycastle.openpgp.operator.KeyFingerPrintCalculator;
public class BcKeyFingerprintCalculator
implements KeyFingerPrintCalculator
{
public byte[] calculateFingerprint(PublicKeyPacket publicPk)
throws PGPException
{
BCPGKey key = publicPk.getKey();
Digest digest;
if (publicPk.getVersion() <= 3)
{
RSAPublicBCPGKey rK = (RSAPublicBCPGKey)key;
try
{
digest = new MD5Digest();
byte[] bytes = new MPInteger(rK.getModulus()).getEncoded();
digest.update(bytes, 2, bytes.length - 2);
bytes = new MPInteger(rK.getPublicExponent()).getEncoded();
digest.update(bytes, 2, bytes.length - 2);
}
catch (IOException e)
{
throw new PGPException("can't encode key components: " + e.getMessage(), e);
}
}
else
{
try
{
byte[] kBytes = publicPk.getEncodedContents();
digest = new SHA1Digest();
digest.update((byte)0x99);
digest.update((byte)(kBytes.length >> 8));
digest.update((byte)kBytes.length);
digest.update(kBytes, 0, kBytes.length);
}
catch (IOException e)
{
throw new PGPException("can't encode key components: " + e.getMessage(), e);
}
}
byte[] digBuf = new byte[digest.getDigestSize()];
digest.doFinal(digBuf, 0);
return digBuf;
}
}
|