|
| 1 | +package org.bouncycastle.bcpg; |
| 2 | + |
| 3 | +public class FingerprintUtil |
| 4 | +{ |
| 5 | + |
| 6 | + /** |
| 7 | + * Derive a 64 bit key-id from a version 6 OpenPGP fingerprint. |
| 8 | + * For v6 keys, the key-id corresponds to the left-most 8 octets of the fingerprint. |
| 9 | + * @param v6Fingerprint 32 byte fingerprint |
| 10 | + * @return key-id |
| 11 | + */ |
| 12 | + public static long keyIdFromV6Fingerprint(byte[] v6Fingerprint) |
| 13 | + { |
| 14 | + return longFromLeftMostBytes(v6Fingerprint); |
| 15 | + } |
| 16 | + |
| 17 | + /** |
| 18 | + * Derive a 64 bit key-id from a version 5 LibrePGP fingerprint. |
| 19 | + * For such keys, the key-id corresponds to the left-most 8 octets of the fingerprint. |
| 20 | + * @param v5Fingerprint 32 byte fingerprint |
| 21 | + * @return key-id |
| 22 | + */ |
| 23 | + public static long keyIdFromLibrePgpFingerprint(byte[] v5Fingerprint) |
| 24 | + { |
| 25 | + return longFromLeftMostBytes(v5Fingerprint); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Derive a 64 bit key-id from a version 4 OpenPGP fingerprint. |
| 30 | + * For v4 keys, the key-id corresponds to the right-most 8 octets of the fingerprint. |
| 31 | + * @param v4Fingerprint 20 byte fingerprint |
| 32 | + * @return key-id |
| 33 | + */ |
| 34 | + public static long keyIdFromV4Fingerprint(byte[] v4Fingerprint) |
| 35 | + { |
| 36 | + return longFromRightMostBytes(v4Fingerprint); |
| 37 | + } |
| 38 | + |
| 39 | + /** |
| 40 | + * Convert the left-most 8 bytes from the given array to a long. |
| 41 | + * @param bytes bytes |
| 42 | + * @return long |
| 43 | + */ |
| 44 | + public static long longFromLeftMostBytes(byte[] bytes) |
| 45 | + { |
| 46 | + if (bytes.length < 8) |
| 47 | + { |
| 48 | + throw new IllegalArgumentException("Byte array MUST contain at least 8 bytes"); |
| 49 | + } |
| 50 | + return ((bytes[0] & 0xffL) << 56) | |
| 51 | + ((bytes[1] & 0xffL) << 48) | |
| 52 | + ((bytes[2] & 0xffL) << 40) | |
| 53 | + ((bytes[3] & 0xffL) << 32) | |
| 54 | + ((bytes[4] & 0xffL) << 24) | |
| 55 | + ((bytes[5] & 0xffL) << 16) | |
| 56 | + ((bytes[6] & 0xffL) << 8) | |
| 57 | + ((bytes[7] & 0xffL)); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Convert the right-most 8 bytes from the given array to a long. |
| 62 | + * @param bytes bytes |
| 63 | + * @return long |
| 64 | + */ |
| 65 | + public static long longFromRightMostBytes(byte[] bytes) |
| 66 | + { |
| 67 | + if (bytes.length < 8) |
| 68 | + { |
| 69 | + throw new IllegalArgumentException("Byte array MUST contain at least 8 bytes"); |
| 70 | + } |
| 71 | + int i = bytes.length; |
| 72 | + return ((bytes[i - 8] & 0xffL) << 56) | |
| 73 | + ((bytes[i - 7] & 0xffL) << 48) | |
| 74 | + ((bytes[i - 6] & 0xffL) << 40) | |
| 75 | + ((bytes[i - 5] & 0xffL) << 32) | |
| 76 | + ((bytes[i - 4] & 0xffL) << 24) | |
| 77 | + ((bytes[i - 3] & 0xffL) << 16) | |
| 78 | + ((bytes[i - 2] & 0xffL) << 8) | |
| 79 | + ((bytes[i - 1] & 0xffL)); |
| 80 | + } |
| 81 | +} |
0 commit comments