|
| 1 | +package org.bouncycastle.pqc.crypto; |
| 2 | + |
| 3 | +import java.io.ByteArrayOutputStream; |
| 4 | + |
| 5 | +import org.bouncycastle.crypto.CipherParameters; |
| 6 | +import org.bouncycastle.crypto.Signer; |
| 7 | +import org.bouncycastle.util.Arrays; |
| 8 | + |
| 9 | +public final class MessageSignerAdapter |
| 10 | + implements Signer |
| 11 | +{ |
| 12 | + private final Buffer buffer = new Buffer(); |
| 13 | + |
| 14 | + private final MessageSigner messageSigner; |
| 15 | + |
| 16 | + public MessageSignerAdapter(MessageSigner messageSigner) |
| 17 | + { |
| 18 | + if (messageSigner == null) |
| 19 | + { |
| 20 | + throw new NullPointerException("'messageSigner' cannot be null"); |
| 21 | + } |
| 22 | + |
| 23 | + this.messageSigner = messageSigner; |
| 24 | + } |
| 25 | + |
| 26 | + public void init(boolean forSigning, CipherParameters param) |
| 27 | + { |
| 28 | + messageSigner.init(forSigning, param); |
| 29 | + } |
| 30 | + |
| 31 | + public void update(byte b) |
| 32 | + { |
| 33 | + buffer.write(b); |
| 34 | + } |
| 35 | + |
| 36 | + public void update(byte[] in, int off, int len) |
| 37 | + { |
| 38 | + buffer.write(in, off, len); |
| 39 | + } |
| 40 | + |
| 41 | + public byte[] generateSignature() |
| 42 | + { |
| 43 | + return messageSigner.generateSignature(getMessage()); |
| 44 | + } |
| 45 | + |
| 46 | + public boolean verifySignature(byte[] signature) |
| 47 | + { |
| 48 | + return messageSigner.verifySignature(getMessage(), signature); |
| 49 | + } |
| 50 | + |
| 51 | + public void reset() |
| 52 | + { |
| 53 | + buffer.reset(); |
| 54 | + } |
| 55 | + |
| 56 | + private byte[] getMessage() |
| 57 | + { |
| 58 | + try |
| 59 | + { |
| 60 | + return buffer.toByteArray(); |
| 61 | + } |
| 62 | + finally |
| 63 | + { |
| 64 | + reset(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private static final class Buffer extends ByteArrayOutputStream |
| 69 | + { |
| 70 | + public synchronized void reset() |
| 71 | + { |
| 72 | + Arrays.fill(buf, 0, count, (byte)0); |
| 73 | + this.count = 0; |
| 74 | + } |
| 75 | + } |
| 76 | +} |
0 commit comments