Java Code Examples for org.spongycastle.crypto.signers.ECDSASigner#verifySignature()

The following examples show how to use org.spongycastle.crypto.signers.ECDSASigner#verifySignature() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ECKeySecp256k1.java    From aion with MIT License 6 votes vote down vote up
/**
 * Verifies the given ECDSA signature against the message bytes using the public key bytes.
 *
 * <p>When using native ECDSA verification, data must be 32 bytes, and no element may be larger
 * than 520 bytes.
 *
 * @param data Hash of the data to verify.
 * @param signature signature.
 * @param pub The public key bytes to use.
 * @return -
 */
public boolean verify(byte[] data, ECDSASignature signature, byte[] pub) {
    ECDSASigner signer = new ECDSASigner();
    ECPublicKeyParameters params =
            new ECPublicKeyParameters(CURVE.getCurve().decodePoint(pub), CURVE);
    signer.init(false, params);
    try {
        return signer.verifySignature(data, signature.r, signature.s);
    } catch (NullPointerException npe) {
        // Bouncy Castle contains a bug that can cause NPEs given specially
        // crafted signatures.
        // Those signatures are inherently invalid/attack sigs so we just
        // fail them here rather than crash the thread.
        logger.error("Caught NPE inside bouncy castle", npe);
        return false;
    }
}
 
Example 2
Source File: Secp256k1.java    From neb.java with GNU Lesser General Public License v3.0 6 votes vote down vote up
public static boolean Verify(byte[] data, byte[] sign, byte[] pub) {
    if (data.length != 32) {
        throw new IllegalArgumentException("Expected 32 byte input to ECDSA verify, not " + data.length);
    }
    if (sign.length != 65) {
        throw new IllegalArgumentException("Expected 65 byte input of signature to ECDSA verify, not " + sign.length);
    }

    ECDSASignature signature = ECDSASignature.fromBytes(sign);

    ECDSASigner signer = new ECDSASigner();
    ECPublicKeyParameters params = new ECPublicKeyParameters(CURVE.getCurve().decodePoint(pub), CURVE);
    signer.init(false, params);
    try {
        return signer.verifySignature(data, signature.r, signature.s);
    } catch (NullPointerException npe) {
        // Bouncy Castle contains a bug that can cause NPEs given specially crafted signatures.
        // Those signatures are inherently invalid/attack sigs so we just fail them here rather than crash the thread.
        return false;
    }
}
 
Example 3
Source File: ECKey.java    From gsc-core with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * <p>Verifies the given ECDSA signature against the message bytes using the public key bytes.</p>
 * <p> <p>When using native ECDSA verification, data must be 32 bytes, and no element may be
 * larger than 520 bytes.</p>
 *
 * @param data      Hash of the data to verify.
 * @param signature signature.
 * @param pub       The public key bytes to use.
 * @return -
 */
public static boolean verify(byte[] data, ECDSASignature signature,
                             byte[] pub) {
    ECDSASigner signer = new ECDSASigner();
    ECPublicKeyParameters params = new ECPublicKeyParameters(CURVE
            .getCurve().decodePoint(pub), CURVE);
    signer.init(false, params);
    try {
        return signer.verifySignature(data, signature.r, signature.s);
    } catch (NullPointerException npe) {
        // Bouncy Castle contains a bug that can cause NPEs given
        // specially crafted signatures.
        // Those signatures are inherently invalid/attack sigs so we just
        // fail them here rather than crash the thread.
        logger.error("Caught NPE inside bouncy castle", npe);
        return false;
    }
}
 
Example 4
Source File: BTCUtils.java    From BlockchainWallet-Crypto with GNU General Public License v3.0 6 votes vote down vote up
public static boolean verify(byte[] publicKey, byte[] signature, byte[] msg) {
    X9ECParameters params = SECNamedCurves.getByName("secp256k1");
    ECDomainParameters EC_PARAMS = new ECDomainParameters(params.getCurve(), params.getG(), params.getN(), params.getH());
    synchronized (EC_PARAMS) {
        boolean valid;
        ECDSASigner signerVer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
        try {
            ECPublicKeyParameters pubKey = new ECPublicKeyParameters(EC_PARAMS.getCurve().decodePoint(publicKey), EC_PARAMS);
            signerVer.init(false, pubKey);
            ASN1InputStream derSigStream = new ASN1InputStream(signature);
            DLSequence seq = (DLSequence) derSigStream.readObject();
            BigInteger r = ((ASN1Integer) seq.getObjectAt(0)).getPositiveValue();
            BigInteger s = ((ASN1Integer) seq.getObjectAt(1)).getPositiveValue();
            derSigStream.close();
            valid = signerVer.verifySignature(msg, r, s);
        } catch (IOException e) {
            throw new RuntimeException();
        }
        return valid;
    }
}
 
Example 5
Source File: ECKey.java    From tron-wallet-android with Apache License 2.0 6 votes vote down vote up
/**
 * <p>Verifies the given ECDSA signature against the message bytes using the public key bytes.</p>
 * <p> <p>When using native ECDSA verification, data must be 32 bytes, and no element may be
 * larger than 520 bytes.</p>
 *
 * @param data Hash of the data to verify.
 * @param signature signature.
 * @param pub The public key bytes to use.
 * @return -
 */
public static boolean verify(byte[] data, ECDSASignature signature,
    byte[] pub) {
  ECDSASigner signer = new ECDSASigner();
  ECPublicKeyParameters params = new ECPublicKeyParameters(CURVE
      .getCurve().decodePoint(pub), CURVE);
  signer.init(false, params);
  try {
    return signer.verifySignature(data, signature.r, signature.s);
  } catch (NullPointerException npe) {
    // Bouncy Castle contains a bug that can cause NPEs given
    // specially crafted signatures.
    // Those signatures are inherently invalid/attack sigs so we just
    // fail them here rather than crash the thread.
    logger.error("Caught NPE inside bouncy castle", npe);
    return false;
  }
}
 
Example 6
Source File: ECKey.java    From asf-sdk with GNU General Public License v3.0 6 votes vote down vote up
/**
 * <p>Verifies the given ECDSA signature against the message bytes using the public key bytes.</p>
 *
 * <p>When using native ECDSA verification, data must be 32 bytes, and no element may be
 * larger than 520 bytes.</p>
 *
 * @param data Hash of the data to verify.
 * @param signature signature.
 * @param pub The public key bytes to use.
 *
 * @return -
 */
public static boolean verify(byte[] data, ECDSASignature signature, byte[] pub) {
  ECDSASigner signer = new ECDSASigner();
  ECPublicKeyParameters params = new ECPublicKeyParameters(CURVE.getCurve()
      .decodePoint(pub), CURVE);
  signer.init(false, params);
  try {
    return signer.verifySignature(data, signature.r, signature.s);
  } catch (NullPointerException npe) {
    // Bouncy Castle contains a bug that can cause NPEs given specially crafted signatures.
    // Those signatures are inherently invalid/attack sigs so we just fail them here rather
    // than crash the thread.
    //            logger.error("Caught NPE inside bouncy castle", npe);
    return false;
  }
}
 
Example 7
Source File: ECKey.java    From bitherj with Apache License 2.0 6 votes vote down vote up
/**
 * <p>Verifies the given ECDSA signature against the message bytes using the public key bytes.</p>
 * <p/>
 * <p>When using native ECDSA verification, data must be 32 bytes, and no element may be
 * larger than 520 bytes.</p>
 *
 * @param data      Hash of the data to verify.
 * @param signature ASN.1 encoded signature.
 * @param pub       The public key bytes to use.
 */
public static boolean verify(byte[] data, ECDSASignature signature, byte[] pub) {
    if (FAKE_SIGNATURES)
        return true;

    if (NativeSecp256k1.enabled)
        return NativeSecp256k1.verify(data, signature.encodeToDER(), pub);

    ECDSASigner signer = new ECDSASigner();
    ECPublicKeyParameters params = new ECPublicKeyParameters(CURVE.getCurve().decodePoint(pub), CURVE);
    signer.init(false, params);
    try {
        return signer.verifySignature(data, signature.r, signature.s);
    } catch (NullPointerException e) {
        // Bouncy Castle contains a bug that can cause NPEs given specially crafted signatures. Those signatures
        // are inherently invalid/attack sigs so we just fail them here rather than crash the thread.
        log.error("Caught NPE inside bouncy castle");
        e.printStackTrace();
        return false;
    }
}
 
Example 8
Source File: ECKey.java    From nuls with MIT License 5 votes vote down vote up
public static boolean verify(byte[] data, ECDSASignature signature, byte[] pub) {
    ECDSASigner signer = new ECDSASigner();
    ECPublicKeyParameters params = new ECPublicKeyParameters(CURVE.getCurve().decodePoint(pub), CURVE);
    signer.init(false, params);
    try {
        return signer.verifySignature(data, signature.r, signature.s);
    } catch (NullPointerException e) {
        log.error("Caught NPE inside bouncy castle", e);
        return false;
    }
}
 
Example 9
Source File: NamedCurve.java    From UAF with Apache License 2.0 5 votes vote down vote up
public static boolean verify(byte[] pub, byte[] dataForSigning,
		BigInteger[] rs) {
	ECDSASigner signer = new ECDSASigner();
	X9ECParameters params = SECNamedCurves.getByName("secp256r1");
	ECDomainParameters ecParams = new ECDomainParameters(params.getCurve(),
			params.getG(), params.getN(), params.getH());
	ECPublicKeyParameters pubKeyParams = new ECPublicKeyParameters(ecParams
			.getCurve().decodePoint(pub), ecParams);
	signer.init(false, pubKeyParams);

	return signer.verifySignature(dataForSigning, rs[0].abs(), rs[1].abs());
}
 
Example 10
Source File: ECKey.java    From ethereumj with MIT License 5 votes vote down vote up
/**
 * <p>Verifies the given ECDSA signature against the message bytes using the public key bytes.</p>
 * 
 * <p>When using native ECDSA verification, data must be 32 bytes, and no element may be
 * larger than 520 bytes.</p>
 *
 * @param data      Hash of the data to verify.
 * @param signature signature.
 * @param pub       The public key bytes to use.
 */
public static boolean verify(byte[] data, ECDSASignature signature, byte[] pub) {
    ECDSASigner signer = new ECDSASigner();
    ECPublicKeyParameters params = new ECPublicKeyParameters(CURVE.getCurve().decodePoint(pub), CURVE);
    signer.init(false, params);
    try {
        return signer.verifySignature(data, signature.r, signature.s);
    } catch (NullPointerException npe) {
        // Bouncy Castle contains a bug that can cause NPEs given specially crafted signatures. 
    	// Those signatures are inherently invalid/attack sigs so we just fail them here rather than crash the thread.
        logger.error("Caught NPE inside bouncy castle", npe);
        return false;
    }
}
 
Example 11
Source File: ECDSAAlgorithm.java    From md_blockchain with Apache License 2.0 3 votes vote down vote up
/**
 * 根据公钥验证签名是否合法
 * @param srcStr
 * 明文字符串
 * @param sign
 * 用私钥签过名的sign
 * @param pubKey
 * 公钥
 * @return
 * 是否校验通过
 * @throws Exception
 * Exception
 */
public static boolean verify(String srcStr, String sign, String pubKey) throws Exception {
    byte[] hash256 = BaseAlgorithm.encode("SHA-256", srcStr.getBytes("UTF-8"));
    ECDSASignature eCDSASignature = ECDSASignature.decodeFromDER(Base64.decodeBase64(sign));
    ECDSASigner signer = new ECDSASigner();
    org.spongycastle.math.ec.ECPoint pub = CURVE.getCurve().decodePoint(Base64.decodeBase64(pubKey));
    ECPublicKeyParameters params = new ECPublicKeyParameters(CURVE.getCurve().decodePoint(pub.getEncoded()), CURVE);
    signer.init(false, params);
    return signer.verifySignature(hash256, eCDSASignature.r, eCDSASignature.s);
}
 
Example 12
Source File: ECKey.java    From wkcwallet-java with Apache License 2.0 3 votes vote down vote up
/**
 * <p>
 * Verifies the given ECDSA signature against the message bytes using the
 * public key bytes.
 * </p>
 *
 * <p>
 * When using native ECDSA verification, data must be 32 bytes, and no
 * element may be larger than 520 bytes.
 * </p>
 *
 * @param data
 *            Hash of the data to verify.
 * @param signature
 *            signature.
 * @param pub
 *            The public key bytes to use.
 *
 * @return -
 */
public static boolean verify(byte[] data, ECDSASignature signature, byte[] pub) {
    ECDSASigner signer = new ECDSASigner();
    ECPublicKeyParameters params = new ECPublicKeyParameters(CURVE.getCurve().decodePoint(pub), CURVE);
    signer.init(false, params);
    try {
        return signer.verifySignature(data, signature.r, signature.s);
    } catch (NullPointerException npe) {
        // Bouncy Castle contains a bug that can cause NPEs given specially
        // crafted signatures.
        // Those signatures are inherently invalid/attack sigs so we just
        // fail them here rather than crash the thread.
        return false;
    }
}