public static SignatureData signMessage(byte[] message, ECKeyPair keyPair, boolean needToHash) { BigInteger publicKey = keyPair.getPublicKey(); byte[] messageHash; if (needToHash) { messageHash = Hash.sha3(message); } else { messageHash = message; } ECDSASignature sig = keyPair.sign(messageHash); // Now we have to work backwards to figure out the recId needed to recover the signature. int recId = -1; for (int i = 0; i < 4; i++) { BigInteger k = recoverFromSignature(i, sig, messageHash); if (k != null && k.equals(publicKey)) { recId = i; break; } } if (recId == -1) { throw new RuntimeException( "Could not construct a recoverable key. Are your credentials valid?"); } int headerByte = recId + 27; // 1 header + 32 bytes for R + 32 bytes for S byte v = (byte) headerByte; byte[] r = Numeric.toBytesPadded(sig.r, 32); byte[] s = Numeric.toBytesPadded(sig.s, 32); return new SignatureData(v, r, s); }
static BigInteger signedMessageHashToKey( byte[] messageHash, SignatureData signatureData) throws SignatureException { byte[] r = signatureData.getR(); byte[] s = signatureData.getS(); verifyPrecondition(r != null && r.length == 32, "r must be 32 bytes"); verifyPrecondition(s != null && s.length == 32, "s must be 32 bytes"); int header = signatureData.getV() & 0xFF; // The header byte: 0x1B = first key with even y, 0x1C = first key with odd y, // 0x1D = second key with even y, 0x1E = second key with odd y if (header < 27 || header > 34) { throw new SignatureException("Header byte out of range: " + header); } ECDSASignature sig = new ECDSASignature( new BigInteger(1, signatureData.getR()), new BigInteger(1, signatureData.getS())); int recId = header - 27; BigInteger key = recoverFromSignature(recId, sig, messageHash); if (key == null) { throw new SignatureException("Could not recover public key from signature"); } return key; }
BigInteger publicKey = Sign.recoverFromSignature( (byte) i, new ECDSASignature(new BigInteger(1, sd.getR()), new BigInteger(1, sd.getS())),
public static SignatureData signMessage(byte[] message, BigInteger publicKey, BigInteger privateKey) { byte[] messageHash = Hash.sha3(message); ECDSASignature sig = sign(messageHash, privateKey); // Now we have to work backwards to figure out the recId needed to recover the signature. int recId = -1; for (int i = 0; i < 4; i++) { BigInteger k = recoverFromSignature(i, sig, messageHash); if (k != null && k.equals(publicKey)) { recId = i; break; } } if (recId == -1) { throw new RuntimeException( "Could not construct a recoverable key. This should never happen."); } int headerByte = recId + 27; // 1 header + 32 bytes for R + 32 bytes for S byte v = (byte) headerByte; byte[] r = Numeric.toBytesPadded(sig.r, 32); byte[] s = Numeric.toBytesPadded(sig.s, 32); return new SignatureData(v, r, s); }
public static SignatureData signMessage(byte[] message, ECKeyPair keyPair, boolean needToHash) { BigInteger publicKey = keyPair.getPublicKey(); byte[] messageHash; if (needToHash) { messageHash = Hash.sha3(message); } else { messageHash = message; } ECDSASignature sig = keyPair.sign(messageHash); // Now we have to work backwards to figure out the recId needed to recover the signature. int recId = -1; for (int i = 0; i < 4; i++) { BigInteger k = recoverFromSignature(i, sig, messageHash); if (k != null && k.equals(publicKey)) { recId = i; break; } } if (recId == -1) { throw new RuntimeException( "Could not construct a recoverable key. Are your credentials valid?"); } int headerByte = recId + 27; // 1 header + 32 bytes for R + 32 bytes for S byte v = (byte) headerByte; byte[] r = Numeric.toBytesPadded(sig.r, 32); byte[] s = Numeric.toBytesPadded(sig.s, 32); return new SignatureData(v, r, s); }
static BigInteger signedMessageHashToKey( byte[] messageHash, SignatureData signatureData) throws SignatureException { byte[] r = signatureData.getR(); byte[] s = signatureData.getS(); verifyPrecondition(r != null && r.length == 32, "r must be 32 bytes"); verifyPrecondition(s != null && s.length == 32, "s must be 32 bytes"); int header = signatureData.getV() & 0xFF; // The header byte: 0x1B = first key with even y, 0x1C = first key with odd y, // 0x1D = second key with even y, 0x1E = second key with odd y if (header < 27 || header > 34) { throw new SignatureException("Header byte out of range: " + header); } ECDSASignature sig = new ECDSASignature( new BigInteger(1, signatureData.getR()), new BigInteger(1, signatureData.getS())); int recId = header - 27; BigInteger key = recoverFromSignature(recId, sig, messageHash); if (key == null) { throw new SignatureException("Could not recover public key from signature"); } return key; }
BigInteger key = recoverFromSignature(recId, sig, messageHash); if (key == null) { throw new SignatureException("Could not recover public key from signature");