This repository was archived by the owner on Jul 15, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSigner.java
More file actions
53 lines (45 loc) · 2.32 KB
/
Signer.java
File metadata and controls
53 lines (45 loc) · 2.32 KB
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
package org.biscuitsec.biscuit.crypto;
import biscuit.format.schema.Schema;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.util.Arrays;
public interface Signer {
byte[] sign(byte[] bytes) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException;
// format: block, algo, publicKey
default byte[] signStandard(byte[] block, Schema.PublicKey.Algorithm algorithm, byte[] publicKey) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
var algorithmBytes = getBufferForAlgorithm(algorithm);
var payload = concatenateArrays(block, algorithmBytes, publicKey);
return sign(payload);
}
// format: block, algo, publicKey, seal
default byte[] signSealed(byte[] block, Schema.PublicKey.Algorithm algorithm, byte[] publicKey, byte[] seal) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
var algorithmBytes = getBufferForAlgorithm(algorithm);
var payload = concatenateArrays(block, algorithmBytes, publicKey, seal);
return sign(payload);
}
// format: block, external, algo, publicKey
default byte[] signExternal(byte[] block, Schema.PublicKey.Algorithm algorithm, byte[] publicKey, byte[] external) throws NoSuchAlgorithmException, SignatureException, InvalidKeyException {
var algorithmBytes = getBufferForAlgorithm(algorithm);
var payload = concatenateArrays(block, external, algorithmBytes, publicKey);
return sign(payload);
}
private static byte[] concatenateArrays(byte[]... arrays) {
int totalLength = Arrays.stream(arrays).mapToInt(arr -> arr.length).sum();
byte[] result = new byte[totalLength];
int currentPos = 0;
for (byte[] array : arrays) {
System.arraycopy(array, 0, result, currentPos, array.length);
currentPos += array.length;
}
return result;
}
private static byte[] getBufferForAlgorithm(Schema.PublicKey.Algorithm algorithm) {
var algorithmBuffer = ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN);
algorithmBuffer.putInt(algorithm.getNumber());
algorithmBuffer.flip();
return algorithmBuffer.array();
}
}