use of snowblossom.proto.SigSpec in project snowblossom by snowblossomcoin.
the class MsgSigUtil method validateSignedMessage.
public static SignedMessagePayload validateSignedMessage(SignedMessage sm, NetworkParams params) throws ValidationException {
try {
SignedMessagePayload payload = SignedMessagePayload.parseFrom(sm.getPayload());
ByteString signature = sm.getSignature();
AddressSpec claim = payload.getClaim();
if ((claim.getRequiredSigners() != 1) || (claim.getSigSpecsCount() != 1)) {
throw new ValidationException("Multisig not supported");
}
MessageDigest md = DigestUtil.getMD();
byte[] hash = md.digest(sm.getPayload().toByteArray());
SigSpec sig_spec = claim.getSigSpecs(0);
if (!SignatureUtil.checkSignature(sig_spec, ByteString.copyFrom(hash), signature)) {
throw new ValidationException("Signature match failure");
}
if (payload.getTimestamp() > params.getMaxClockSkewMs() + System.currentTimeMillis()) {
throw new ValidationException("Signed message too far into future");
}
return payload;
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
throw new ValidationException(e);
}
}
use of snowblossom.proto.SigSpec in project snowblossom by snowblossomcoin.
the class AddressUtilTest method testBasicSpecHash.
@Test
public void testBasicSpecHash() {
AddressSpec.Builder b = AddressSpec.newBuilder();
ByteBuffer bb = ByteBuffer.allocate(1024 * 1024);
Random rnd = new Random();
int keys = rnd.nextInt(100);
int required = 0;
if (keys > 0)
required = rnd.nextInt(keys) + 1;
b.setRequiredSigners(required);
bb.putInt(required);
bb.putInt(keys);
for (int i = 0; i < keys; i++) {
byte[] public_key = new byte[rnd.nextInt(800)];
rnd.nextBytes(public_key);
int sig_type = rnd.nextInt(12);
bb.putInt(sig_type);
bb.putInt(public_key.length);
bb.put(public_key);
SigSpec ss = SigSpec.newBuilder().setSignatureType(sig_type).setPublicKey(ByteString.copyFrom(public_key)).build();
b.addSigSpecs(ss);
}
AddressSpecHash found_hash = AddressUtil.getHashForSpec(b.build(), DigestUtil.getMDAddressSpec());
MessageDigest md = DigestUtil.getMDAddressSpec();
md.update(bb.array(), 0, bb.position());
byte[] h = md.digest();
AddressSpecHash expected_hash = new AddressSpecHash(h);
Assert.assertEquals(expected_hash, found_hash);
System.out.println("" + found_hash + " " + expected_hash);
}
use of snowblossom.proto.SigSpec in project snowblossom by snowblossomcoin.
the class CipherUtilTest method testKeys.
private void testKeys(WalletKeyPair wkp) throws Exception {
SigSpec sig_spec = SigSpec.newBuilder().setSignatureType(wkp.getSignatureType()).setPublicKey(wkp.getPublicKey()).build();
Random rnd = new Random();
for (int i = 0; i < 100; i++) {
byte[] b = new byte[rnd.nextInt(100000)];
if (i == 0)
b = new byte[0];
rnd.nextBytes(b);
ByteString input = ByteString.copyFrom(b);
ByteString output = CipherUtil.encrypt(sig_spec, input);
ByteString output2 = CipherUtil.encrypt(sig_spec, input);
Assert.assertTrue(output.size() >= input.size());
Assert.assertFalse(output.equals(output2));
ByteString dec = CipherUtil.decrypt(wkp, output);
Assert.assertEquals(input, dec);
}
}
use of snowblossom.proto.SigSpec in project snowblossom by snowblossomcoin.
the class KeyUtilTest method testKeyPair.
private void testKeyPair(WalletKeyPair wkp, String name) throws Exception {
Random rnd = new Random();
byte[] b = new byte[Globals.BLOCKCHAIN_HASH_LEN];
for (int i = 0; i < 8; i++) {
rnd.nextBytes(b);
ChainHash hash = new ChainHash(b);
ByteString sig = SignatureUtil.sign(wkp, hash);
SigSpec sig_spec = SigSpec.newBuilder().setSignatureType(wkp.getSignatureType()).setPublicKey(wkp.getPublicKey()).build();
logger.info(String.format("Key report %s Pub size: %d, sig %d", name, wkp.getPublicKey().size(), sig.size()));
logger.info("Key report: " + HexUtil.getHexString(sig));
// logger.info("Key report: " + KeyUtil.decomposeASN1Encoded( sig ));
Assert.assertTrue(SignatureUtil.checkSignature(sig_spec, hash.getBytes(), sig));
}
}
use of snowblossom.proto.SigSpec in project snowblossom by snowblossomcoin.
the class AddressUtil method getAddressSpecTypeSummary.
public static String getAddressSpecTypeSummary(AddressSpec spec) throws ValidationException {
String multi = String.format("%dof%d", spec.getRequiredSigners(), spec.getSigSpecsCount());
StringBuilder algo_str = new StringBuilder();
for (int s = 0; s < spec.getSigSpecsCount(); s++) {
SigSpec sig = spec.getSigSpecs(s);
String algo = SignatureUtil.getAlgo(sig.getSignatureType());
if (s > 0)
algo_str.append(" ");
algo_str.append(algo);
}
return multi + " " + algo_str;
}
Aggregations