1
1
package org .bouncycastle .bcpg ;
2
2
3
+ import org .bouncycastle .util .Pack ;
4
+
3
5
public class FingerprintUtil
4
6
{
5
7
@@ -47,18 +49,7 @@ public static long keyIdFromV4Fingerprint(byte[] v4Fingerprint)
47
49
*/
48
50
public static long longFromLeftMostBytes (byte [] bytes )
49
51
{
50
- if (bytes .length < 8 )
51
- {
52
- throw new IllegalArgumentException ("Byte array MUST contain at least 8 bytes" );
53
- }
54
- return ((bytes [0 ] & 0xffL ) << 56 ) |
55
- ((bytes [1 ] & 0xffL ) << 48 ) |
56
- ((bytes [2 ] & 0xffL ) << 40 ) |
57
- ((bytes [3 ] & 0xffL ) << 32 ) |
58
- ((bytes [4 ] & 0xffL ) << 24 ) |
59
- ((bytes [5 ] & 0xffL ) << 16 ) |
60
- ((bytes [6 ] & 0xffL ) << 8 ) |
61
- ((bytes [7 ] & 0xffL ));
52
+ return readKeyID (bytes );
62
53
}
63
54
64
55
/**
@@ -68,19 +59,57 @@ public static long longFromLeftMostBytes(byte[] bytes)
68
59
* @return long
69
60
*/
70
61
public static long longFromRightMostBytes (byte [] bytes )
62
+ {
63
+ return readKeyID (bytes , bytes .length - 8 );
64
+ }
65
+
66
+ /**
67
+ * Read a key-ID from the first 8 octets of the given byte array.
68
+ * @param bytes byte array
69
+ * @return key-ID
70
+ */
71
+ public static long readKeyID (byte [] bytes )
72
+ {
73
+ return readKeyID (bytes , 0 );
74
+ }
75
+
76
+ /**
77
+ * Read a key-ID from 8 octets of the given byte array starting at offset.
78
+ * @param bytes byte array
79
+ * @param offset offset
80
+ * @return key-ID
81
+ */
82
+ public static long readKeyID (byte [] bytes , int offset )
71
83
{
72
84
if (bytes .length < 8 )
73
85
{
74
86
throw new IllegalArgumentException ("Byte array MUST contain at least 8 bytes" );
75
87
}
76
- int i = bytes .length ;
77
- return ((bytes [i - 8 ] & 0xffL ) << 56 ) |
78
- ((bytes [i - 7 ] & 0xffL ) << 48 ) |
79
- ((bytes [i - 6 ] & 0xffL ) << 40 ) |
80
- ((bytes [i - 5 ] & 0xffL ) << 32 ) |
81
- ((bytes [i - 4 ] & 0xffL ) << 24 ) |
82
- ((bytes [i - 3 ] & 0xffL ) << 16 ) |
83
- ((bytes [i - 2 ] & 0xffL ) << 8 ) |
84
- ((bytes [i - 1 ] & 0xffL ));
88
+ return Pack .bigEndianToLong (bytes , offset );
89
+ }
90
+
91
+ /**
92
+ * Write the key-ID encoded as 8 octets to the given byte array, starting at index offset.
93
+ * @param keyID keyID
94
+ * @param bytes byte array
95
+ * @param offset starting offset
96
+ */
97
+ public static void writeKeyID (long keyID , byte [] bytes , int offset )
98
+ {
99
+ if (bytes .length - offset < 8 )
100
+ {
101
+ throw new IllegalArgumentException ("Not enough space to write key-ID to byte array." );
102
+ }
103
+ Pack .longToBigEndian (keyID , bytes , offset );
104
+ }
105
+
106
+ /**
107
+ * Write the key-ID to the first 8 octets of the given byte array.
108
+ * @param keyID keyID
109
+ * @param bytes byte array
110
+ */
111
+ public static void writeKeyID (long keyID , byte [] bytes )
112
+ {
113
+ writeKeyID (keyID , bytes , 0 );
85
114
}
86
115
}
0 commit comments