當前位置: 首頁>>代碼示例>>Java>>正文


Java Signature.verify方法代碼示例

本文整理匯總了Java中java.security.Signature.verify方法的典型用法代碼示例。如果您正苦於以下問題:Java Signature.verify方法的具體用法?Java Signature.verify怎麽用?Java Signature.verify使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.security.Signature的用法示例。


在下文中一共展示了Signature.verify方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: verify

import java.security.Signature; //導入方法依賴的package包/類
private static void verify(Provider provider, String alg, PublicKey key,
        byte[] data, byte[] sig, boolean result) throws Exception {
    Signature s = Signature.getInstance(alg, provider);
    s.initVerify(key);
    boolean r;
    s.update(data);
    r = s.verify(sig);
    if (r != result) {
        throw new Exception("Result mismatch, actual: " + r);
    }
    s.update(data);
    r = s.verify(sig);
    if (r != result) {
        throw new Exception("Result mismatch, actual: " + r);
    }
    System.out.println("Passed");
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:18,代碼來源:TestECDSA.java

示例2: validateSignBySoft256

import java.security.Signature; //導入方法依賴的package包/類
public static boolean validateSignBySoft256(PublicKey publicKey,
		byte[] signData, byte[] srcData) throws Exception {
	Signature st = Signature.getInstance(BC_PROV_ALGORITHM_SHA256RSA, "BC");
	st.initVerify(publicKey);
	st.update(srcData);
	return st.verify(signData);
}
 
開發者ID:wangfei0904306,項目名稱:unionpay,代碼行數:8,代碼來源:SecureUtil.java

示例3: testInvalidSignature

import java.security.Signature; //導入方法依賴的package包/類
private static void testInvalidSignature(KeyPair kp1, KeyPair kp2) throws Exception {
    System.out.println("Testing signature with incorrect key...");
    Signature sig = Signature.getInstance("MD5withRSA", provider);
    sig.initSign(kp1.getPrivate());
    byte[] data = new byte[100];
    sig.update(data);
    byte[] signature = sig.sign();
    sig.initVerify(kp1.getPublic());
    sig.update(data);
    if (sig.verify(signature) == false) {
        throw new Exception("verification failed");
    }
    sig.initVerify(kp2.getPublic());
    sig.update(data);
    // verify needs to return false and not throw an Exception
    if (sig.verify(signature)) {
        throw new Exception("verification unexpectedly succeeded");
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:20,代碼來源:TestKeyPairGenerator.java

示例4: createDecoder

import java.security.Signature; //導入方法依賴的package包/類
@Override
public OneWayCodec createDecoder() throws Exception {
    final Signature signature = Signature.getInstance(signatureAlgorithmName);
    signature.initVerify(keyPair.getPublic());

    return new OneWayCodec() {

        @Override
        public byte[] code(final byte[] data) throws Exception {
            final int dataLen = data.length - signatureLength;
            signature.update(data, 0, dataLen);
            if (!signature.verify(data, dataLen, signatureLength)) {
                throw new FlowStateStorageException("Invalid signature");
            }
            final byte[] b = new byte[dataLen];
            System.arraycopy(data, 0, b, 0, dataLen);
            return b;
        }
    };
}
 
開發者ID:szegedi,項目名稱:spring-web-jsflow,代碼行數:21,代碼來源:IntegrityCodec.java

示例5: verify

import java.security.Signature; //導入方法依賴的package包/類
/**
 * Verifies that this CRL was signed using the
 * private key that corresponds to the given public key,
 * and that the signature verification was computed by
 * the given provider. Note that the specified Provider object
 * does not have to be registered in the provider list.
 *
 * @param key the PublicKey used to carry out the verification.
 * @param sigProvider the signature provider.
 *
 * @exception NoSuchAlgorithmException on unsupported signature
 * algorithms.
 * @exception InvalidKeyException on incorrect key.
 * @exception SignatureException on signature errors.
 * @exception CRLException on encoding errors.
 */
public synchronized void verify(PublicKey key, Provider sigProvider)
        throws CRLException, NoSuchAlgorithmException, InvalidKeyException,
        SignatureException {

    if (signedCRL == null) {
        throw new CRLException("Uninitialized CRL");
    }
    Signature sigVerf = null;
    if (sigProvider == null) {
        sigVerf = Signature.getInstance(sigAlgId.getName());
    } else {
        sigVerf = Signature.getInstance(sigAlgId.getName(), sigProvider);
    }
    sigVerf.initVerify(key);

    if (tbsCertList == null) {
        throw new CRLException("Uninitialized CRL");
    }

    sigVerf.update(tbsCertList, 0, tbsCertList.length);

    if (!sigVerf.verify(signature)) {
        throw new SignatureException("Signature does not match.");
    }
    verifiedPublicKey = key;
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:43,代碼來源:X509CRLImpl.java

示例6: verify

import java.security.Signature; //導入方法依賴的package包/類
/**
 * 
 * if has performance problem ,change Signature to ThreadLocal instance  
 * @param publicKey public key after base64 encode 
 * @param sign 簽名
 * @param content original content 
 * @return verify result
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 * @throws InvalidKeyException
 * @throws SignatureException
 */
public static boolean verify(String publicKey, String sign, String content)
    throws NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException {
  if (null == kf) {
    throw new NoSuchAlgorithmException(RSA_ALG + " KeyFactory not available");
  }
  byte[] bytes = decoder.decode(publicKey);
  X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes);
  PublicKey pubKey = kf.generatePublic(keySpec);
  Signature signature = Signature.getInstance(SIGN_ALG);
  signature.initVerify(pubKey);
  signature.update(content.getBytes());
  return signature.verify(decoder.decode(sign));
}
 
開發者ID:apache,項目名稱:incubator-servicecomb-java-chassis,代碼行數:26,代碼來源:RSAUtils.java

示例7: validateSignBySoft

import java.security.Signature; //導入方法依賴的package包/類
public static boolean validateSignBySoft(PublicKey publicKey,
		byte[] signData, byte[] srcData) throws Exception {
	Signature st = Signature.getInstance(BC_PROV_ALGORITHM_SHA1RSA, "BC");
	st.initVerify(publicKey);
	st.update(srcData);
	return st.verify(signData);
}
 
開發者ID:wangfei0904306,項目名稱:unionpay,代碼行數:8,代碼來源:SecureUtil.java

示例8: testSignAndVerify

import java.security.Signature; //導入方法依賴的package包/類
private static void testSignAndVerify(String alg, KeyPair kp, Provider p) throws Exception {
    Signature s = Signature.getInstance(alg, p);
    s.initSign(kp.getPrivate());
    s.update(data);
    byte[] result = s.sign();

    s.initVerify(kp.getPublic());
    s.update(data);
    if (!s.verify(result)) {
        throw new Exception("Error: Signature verification failed");
    }
    System.out.println(p.getName() + ": " + alg + " Passed");
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:14,代碼來源:TestECDSA2.java

示例9: verify

import java.security.Signature; //導入方法依賴的package包/類
protected static boolean verify(Class signatureClass, byte[] signatureData, Object entity, User signee, Timestamp timestamp, int depth, StringBuilder comment) throws Exception {
	Signature signature = getSignature();
	signature.initVerify(CryptoUtil.getPublicKey(signee.getKeyPair().getPublicKey()));
	updateSignature(entity, getEntitySignatures(signatureClass, entity.getClass(), depth), signature, signee, timestamp, comment);
	//FileUtils.writeStringToFile(new java.io.File("d:\\ctsms\\verify_" + timestamp.getTime() + ".txt"),comment.toString());
	return signature.verify(signatureData);
}
 
開發者ID:phoenixctms,項目名稱:ctsms,代碼行數:8,代碼來源:EntitySignature.java

示例10: verify

import java.security.Signature; //導入方法依賴的package包/類
public void verify(PublicKey key, String sigProvider)
    throws CRLException, NoSuchAlgorithmException,
        InvalidKeyException, NoSuchProviderException, SignatureException
{
    if (!c.getSignatureAlgorithm().equals(c.getTBSCertList().getSignature()))
    {
        throw new CRLException("Signature algorithm on CertificateList does not match TBSCertList.");
    }

    Signature sig;

    if (sigProvider != null)
    {
        sig = Signature.getInstance(getSigAlgName(), sigProvider);
    }
    else
    {
        sig = Signature.getInstance(getSigAlgName());
    }

    sig.initVerify(key);
    sig.update(this.getTBSCertList());

    if (!sig.verify(this.getSignature()))
    {
        throw new SignatureException("CRL does not verify with supplied public key.");
    }
}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:29,代碼來源:X509CRLObject.java

示例11: testSignature

import java.security.Signature; //導入方法依賴的package包/類
private static void testSignature(String algorithm, PrivateKey privateKey,
        PublicKey publicKey) throws Exception {
    System.out.println("Testing " + algorithm + "...");
    Signature s = Signature.getInstance(algorithm, provider);
    s.initSign(privateKey);
    s.update(data);
    byte[] sig = s.sign();
    s.initVerify(publicKey);
    s.update(data);
    boolean result = s.verify(sig);
    if (result == false) {
        throw new Exception("Verification failed");
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:15,代碼來源:TestKeyPairGenerator.java

示例12: verify

import java.security.Signature; //導入方法依賴的package包/類
/**
 * 校驗數字簽名
 *
 * @param data      加密數據
 * @param publicKey 公鑰
 * @param sign      數字簽名
 * @return
 * @throws Exception
 */
public static boolean verify(byte[] data, String publicKey, String sign)
        throws Exception {

    byte[] keyBytes = BASE64.decode(publicKey); // 解密由base64編碼的公鑰
    X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);  // 構造X509EncodedKeySpec對象
    KeyFactory keyFactory = KeyFactory.getInstance(Algorithm.RSA.getType());  // KEY_ALGORITHM 指定的加密算法
    PublicKey pubKey = keyFactory.generatePublic(keySpec);   // 取公鑰對象

    Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
    signature.initVerify(pubKey);
    signature.update(data);

    return signature.verify(BASE64.decode(sign));
}
 
開發者ID:lzmlsfe,項目名稱:19porn,代碼行數:24,代碼來源:Codec.java

示例13: verify

import java.security.Signature; //導入方法依賴的package包/類
public boolean verify() throws NoSuchAlgorithmException,
        NoSuchProviderException, InvalidKeySpecException,
        InvalidKeyException, SignatureException {
    final X509EncodedKeySpec pubKeySpec = new X509EncodedKeySpec(encKey);
    final KeyFactory keyFactory = KeyFactory.getInstance("DSA", "SUN");
    final PublicKey pubKey = keyFactory.generatePublic(pubKeySpec);
    final byte[] sigToVerify = sign;
    final Signature sig = Signature.getInstance("SHA1withDSA", "SUN");
    sig.initVerify(pubKey);
    sig.update(data, 0, data.length);
    final boolean verifies = sig.verify(sigToVerify);
    return verifies;
}
 
開發者ID:Vitaliy-Yakovchuk,項目名稱:ramus,代碼行數:14,代碼來源:KeyVerificator.java

示例14: PKCS10

import java.security.Signature; //導入方法依賴的package包/類
/**
 * Parses an encoded, signed PKCS #10 certificate request, verifying
 * the request's signature as it does so.  This constructor would
 * typically be used by a Certificate Authority, from which a new
 * certificate would then be constructed.
 *
 * @param data the DER-encoded PKCS #10 request.
 * @exception IOException for low level errors reading the data
 * @exception SignatureException when the signature is invalid
 * @exception NoSuchAlgorithmException when the signature
 *  algorithm is not supported in this environment
 */
public PKCS10(byte[] data)
throws IOException, SignatureException, NoSuchAlgorithmException {
    DerInputStream  in;
    DerValue[]      seq;
    AlgorithmId     id;
    byte[]          sigData;
    Signature       sig;

    encoded = data;

    //
    // Outer sequence:  request, signature algorithm, signature.
    // Parse, and prepare to verify later.
    //
    in = new DerInputStream(data);
    seq = in.getSequence(3);

    if (seq.length != 3)
        throw new IllegalArgumentException("not a PKCS #10 request");

    data = seq[0].toByteArray();            // reusing this variable
    id = AlgorithmId.parse(seq[1]);
    sigData = seq[2].getBitString();

    //
    // Inner sequence:  version, name, key, attributes
    //
    BigInteger      serial;
    DerValue        val;

    serial = seq[0].data.getBigInteger();
    if (!serial.equals(BigInteger.ZERO))
        throw new IllegalArgumentException("not PKCS #10 v1");

    subject = new X500Name(seq[0].data);
    subjectPublicKeyInfo = X509Key.parse(seq[0].data.getDerValue());

    // Cope with a somewhat common illegal PKCS #10 format
    if (seq[0].data.available() != 0)
        attributeSet = new PKCS10Attributes(seq[0].data);
    else
        attributeSet = new PKCS10Attributes();

    if (seq[0].data.available() != 0)
        throw new IllegalArgumentException("illegal PKCS #10 data");

    //
    // OK, we parsed it all ... validate the signature using the
    // key and signature algorithm we found.
    //
    try {
        sigAlg = id.getName();
        sig = Signature.getInstance(sigAlg);
        sig.initVerify(subjectPublicKeyInfo);
        sig.update(data);
        if (!sig.verify(sigData))
            throw new SignatureException("Invalid PKCS #10 signature");
    } catch (InvalidKeyException e) {
        throw new SignatureException("invalid key");
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:74,代碼來源:PKCS10.java

示例15: PKCS10

import java.security.Signature; //導入方法依賴的package包/類
/**
 * Parses an encoded, signed PKCS #10 certificate request, verifying
 * the request's signature as it does so.  This constructor would
 * typically be used by a Certificate Authority, from which a new
 * certificate would then be constructed.
 *
 * @param data the DER-encoded PKCS #10 request.
 * @exception IOException for low level errors reading the data
 * @exception SignatureException when the signature is invalid
 * @exception NoSuchAlgorithmException when the signature
 *  algorithm is not supported in this environment
 */
public PKCS10(byte[] data)
throws IOException, SignatureException, NoSuchAlgorithmException {
    DerInputStream  in;
    DerValue[]      seq;
    AlgorithmId     id;
    byte[]          sigData;
    Signature       sig;

    encoded = data;

    //
    // Outer sequence:  request, signature algorithm, signature.
    // Parse, and prepare to verify later.
    //
    in = new DerInputStream(data);
    seq = in.getSequence(3);

    if (seq.length != 3)
        throw new IllegalArgumentException("not a PKCS #10 request");

    data = seq[0].toByteArray();            // reusing this variable
    id = AlgorithmId.parse(seq[1]);
    sigData = seq[2].getBitString();

    //
    // Inner sequence:  version, name, key, attributes
    //
    BigInteger      serial;
    DerValue        val;

    serial = seq[0].data.getBigInteger();
    if (!serial.equals(BigInteger.ZERO))
        throw new IllegalArgumentException("not PKCS #10 v1");

    subject = new X500Name(seq[0].data);
    subjectPublicKeyInfo = X509Key.parse(seq[0].data.getDerValue());

    // Cope with a somewhat common illegal PKCS #10 format
    if (seq[0].data.available() != 0)
        attributeSet = new PKCS10Attributes(seq[0].data);
    else
        attributeSet = new PKCS10Attributes();

    if (seq[0].data.available() != 0)
        throw new IllegalArgumentException("illegal PKCS #10 data");

    //
    // OK, we parsed it all ... validate the signature using the
    // key and signature algorithm we found.
    //
    try {
        sig = Signature.getInstance(id.getName());
        sig.initVerify(subjectPublicKeyInfo);
        sig.update(data);
        if (!sig.verify(sigData))
            throw new SignatureException("Invalid PKCS #10 signature");
    } catch (InvalidKeyException e) {
        throw new SignatureException("invalid key");
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:73,代碼來源:PKCS10.java


注:本文中的java.security.Signature.verify方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。