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


Java Signature.initVerify方法代碼示例

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


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

示例1: signatureTest

import java.security.Signature; //導入方法依賴的package包/類
private void signatureTest(ValueSigner signer, KeyPairGenerator generator) throws Exception {
  byte[] data = "HELLO".getBytes();
  Signature signature = signer.getInstance(null);
  KeyPair pair = generator.generateKeyPair();

  signature.initSign(pair.getPrivate());
  signature.update(data);
  byte[] digest = signature.sign();

  assertThat("digest has a value", TestUtils.wrap(digest), is(not(emptyArray())));

  signature.initVerify(pair.getPublic());
  signature.update(data);
  assertThat("verification succeeds", signature.verify(digest), is(true));

  switch (signer) {
    case RSA_PKCS1:
      break;

    default:
      signature.initSign(pair.getPrivate());
      signature.update(data);
      assertThat("signatures correctly use random padding", signature.sign(), not(equalTo(digest)));
  }
}
 
開發者ID:mit-ll,項目名稱:PACE,代碼行數:26,代碼來源:ValueSignerTest.java

示例2: verify

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

	// 解密由base64編碼的公鑰
	byte[] keyBytes = decryptBASE64(publicKey);

	// 構造X509EncodedKeySpec對象
	X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);

	// KEY_ALGORITHM 指定的加密算法
	KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);

	// 取公鑰匙對象
	PublicKey pubKey = keyFactory.generatePublic(keySpec);

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

	// 驗證簽名是否正常
	return signature.verify(decryptBASE64(sign));
}
 
開發者ID:wooui,項目名稱:springboot-training,代碼行數:36,代碼來源:RSACoder.java

示例3: verifyKeyBlock

import java.security.Signature; //導入方法依賴的package包/類
public static boolean
verifyKeyBlock(
	byte[]		request,
	byte[]		signature )
{
	try{
		Signature	verifier = Signature.getInstance("MD5withRSA" );

		verifier.initVerify( key_block_public_key );

		verifier.update( request );

		if ( !verifier.verify( signature )){

			return( false );
		}

		return( true );

	}catch( Throwable e ){

		return( false );
	}
}
 
開發者ID:BiglySoftware,項目名稱:BiglyBT,代碼行數:25,代碼來源:DHTPluginStorageManager.java

示例4: verifySignature

import java.security.Signature; //導入方法依賴的package包/類
private boolean verifySignature(byte[] signingInput, byte[] signature, PublicKey publicKey)
{
    try
    {
        Signature verifier = Signature.getInstance("SHA256withRSA");

        verifier.initVerify(publicKey);
        verifier.update(signingInput);

        return verifier.verify(signature);
    }
    catch (Exception e)
    {
        throw new RuntimeException("Unable to validate JWT signature", e);
    }
}
 
開發者ID:curityio,項目名稱:oauth-filter-for-java,代碼行數:17,代碼來源:AbstractJwtValidator.java

示例5: 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.getBytes(), Base64.DEFAULT);
    //構造X509EncodedKeySpec對象
    X509EncodedKeySpec x509EncodedKeySpec = new X509EncodedKeySpec(keyBytes);
    //指定加密算法
    KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
    //取公鑰匙對象
    PublicKey publicKey2 = keyFactory.generatePublic(x509EncodedKeySpec);

    Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
    signature.initVerify(publicKey2);
    signature.update(data);
    //驗證簽名是否正常
    return signature.verify(Base64.decode(sign, Base64.DEFAULT));

}
 
開發者ID:abook23,項目名稱:godlibrary,代碼行數:27,代碼來源:RSAUtlis.java

示例6: verify

import java.security.Signature; //導入方法依賴的package包/類
public boolean verify(String challenge) throws NoSuchAlgorithmException,
        InvalidKeyException, SignatureException, NoSuchProviderException
{
    if (!challenge.equals(this.challenge))
    {
        return false;
    }

    //
    // Verify the signature .. shows the response was generated
    // by someone who knew the associated private key
    //
    Signature sig = Signature.getInstance(sigAlg.getObjectId().getId(),
            "BC");
    sig.initVerify(pubkey);
    sig.update(content.getBytes());

    return sig.verify(sigBits);
}
 
開發者ID:Appdome,項目名稱:ipack,代碼行數:20,代碼來源:NetscapeCertRequest.java

示例7: 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

示例8: 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:howe,項目名稱:nutz-pay,代碼行數:8,代碼來源:SecureUtil.java

示例9: main

import java.security.Signature; //導入方法依賴的package包/類
public static void main(String[] args) throws Exception {
    KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("DSA");
    keyPairGenerator.initialize(1024);
    KeyPair keys = keyPairGenerator.generateKeyPair();
    PublicKey publicKey = keys.getPublic();
    byte[] sigBytes = new byte[100];

    Signature signature = Signature.getInstance("SHA1withDSA");
    signature.initVerify(publicKey);
    try {
        signature.verify(sigBytes, Integer.MAX_VALUE, 1);
    } catch (IllegalArgumentException ex) {
        // Expected
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:16,代碼來源:VerifyRangeCheckOverflow.java

示例10: verifyUserSig

import java.security.Signature; //導入方法依賴的package包/類
@Override
public boolean verifyUserSig(String identifier, String sig)throws QCloudException {
	try {
		Security.addProvider(new BouncyCastleProvider());
		
		//DeBaseUrl64 urlSig to json
		Base64 decoder = new Base64();

		byte [] compressBytes = Base64Url.base64DecodeUrl(sig.getBytes(Charset.forName("UTF-8")));
		
		//Decompression
		Inflater decompression =  new Inflater();
		decompression.setInput(compressBytes, 0, compressBytes.length);
		byte [] decompressBytes = new byte [1024];
		int decompressLength = decompression.inflate(decompressBytes);
		decompression.end();
		
		String jsonString = new String(Arrays.copyOfRange(decompressBytes, 0, decompressLength));
		
		//Get TLS.Sig from json
		JSONObject jsonObject= JSON.parseObject(jsonString);
		String sigTLS = jsonObject.getString("TLS.sig");
		
		//debase64 TLS.Sig to get serailString
		byte[] signatureBytes = decoder.decode(sigTLS.getBytes(Charset.forName("UTF-8")));
		
		String strSdkAppid = jsonObject.getString("TLS.sdk_appid");
		String sigTime = jsonObject.getString("TLS.time");
		String sigExpire = jsonObject.getString("TLS.expire_after");
		
		if (!imConfig.getSdkAppId().equals(strSdkAppid))
		{
			return false;
		}

		if ( System.currentTimeMillis()/1000 - Long.parseLong(sigTime) > Long.parseLong(sigExpire)) {
			return false;
		}
		
		//Get Serial String from json
		String SerialString = 
			"TLS.appid_at_3rd:" + 0 + "\n" +
			"TLS.account_type:" + 0 + "\n" +
			"TLS.identifier:" + identifier + "\n" + 
			"TLS.sdk_appid:" + imConfig.getSdkAppId() + "\n" + 
			"TLS.time:" + sigTime + "\n" + 
			"TLS.expire_after:" + sigExpire + "\n";
	
        Reader reader = new CharArrayReader(imConfig.getPublicKey().toCharArray());
        PEMParser  parser = new PEMParser(reader);
        JcaPEMKeyConverter converter = new JcaPEMKeyConverter();
        Object obj = parser.readObject();
        parser.close();
        PublicKey pubKeyStruct  = converter.getPublicKey((SubjectPublicKeyInfo) obj);

		Signature signature = Signature.getInstance("SHA256withECDSA","BC");
		signature.initVerify(pubKeyStruct);
		signature.update(SerialString.getBytes(Charset.forName("UTF-8")));
		return signature.verify(signatureBytes);
	}catch (Exception e) {
		throw new QCloudException(e);
	}
}
 
開發者ID:51wakeup,項目名稱:wakeup-qcloud-sdk,代碼行數:64,代碼來源:DefaultQCloudClient.java

示例11: checkSignature

import java.security.Signature; //導入方法依賴的package包/類
private static void checkSignature(byte[] data, PublicKey pub,
        PrivateKey priv, String sigalg) throws NoSuchAlgorithmException,
        InvalidKeyException, SignatureException, NoSuchProviderException {
    Signature sig = Signature.getInstance(sigalg, PROVIDER);
    sig.initSign(priv);
    for (int i = 0; i < UPDATE_TIMES_HUNDRED; i++) {
        sig.update(data);
    }
    byte[] signedData = sig.sign();

    // Make sure signature verifies with original data
    sig.initVerify(pub);
    for (int i = 0; i < UPDATE_TIMES_HUNDRED; i++) {
        sig.update(data);
    }
    if (!sig.verify(signedData)) {
        throw new RuntimeException("Failed to verify " + sigalg
                + " signature");
    }

    // Make sure signature does NOT verify when the original data
    // has changed
    sig.initVerify(pub);
    for (int i = 0; i < UPDATE_TIMES_FIFTY; i++) {
        sig.update(data);
    }

    if (sig.verify(signedData)) {
        throw new RuntimeException("Failed to detect bad " + sigalg
                + " signature");
    }
}
 
開發者ID:lambdalab-mirror,項目名稱:jdk8u-jdk,代碼行數:33,代碼來源:SignatureTest.java

示例12: verifyMessage

import java.security.Signature; //導入方法依賴的package包/類
@Override
public boolean verifyMessage(byte[] bytes, byte[] signature)
    throws GeneralSecurityException {
  if (bytes == null) {
    throw new IllegalArgumentException("bytes must not be null");
  }
  if (signature == null) {
    throw new IllegalArgumentException("signature must not be null");
  }
  if (signature.length != SIGNATURE_LENGTH_BYTES) {
    return false;
  }
  byte[] first = new byte[signature.length / 2];
  byte[] second = new byte[signature.length / 2];
  System.arraycopy(signature, 0, first, 0, first.length);
  System.arraycopy(signature, first.length, second, 0, second.length);
  BigInteger r = new BigInteger(Utils.byte2Hex(first), 16);
  BigInteger s = new BigInteger(Utils.byte2Hex(second), 16);
  // This is awful, but encoding an extra 0 byte works better on devices.
  byte[] encoded = ASNUtils.encodeTwoArraysToASN1(
      Utils.hex2Byte(r.toString(16), 1 + SIGNATURE_LENGTH_BYTES / 2),
      Utils.hex2Byte(s.toString(16), 1 + SIGNATURE_LENGTH_BYTES / 2));

  final Signature signer = Signature.getInstance(SIGNATURE_ALGORITHM);
  signer.initVerify(publicKey);
  signer.update(bytes);
  return signer.verify(encoded);
}
 
開發者ID:mozilla-mobile,項目名稱:FirefoxData-android,代碼行數:29,代碼來源:DSACryptoImplementation.java

示例13: validateJWT

import java.security.Signature; //導入方法依賴的package包/類
public void validateJWT(String authHeader, PublicKey publicKey) {
  assertNotNull("Authorization header was not present in response", authHeader);
  assertTrue("Authorization header does not contain a bearer", authHeader.startsWith("Bearer "));

  StringTokenizer st = new StringTokenizer(authHeader.substring(7), ".");
  assertTrue("JWT does not contain three parts", st.countTokens() == 3);

  String jwtHeaderEnc = st.nextToken();
  String jwtClaimsEnc = st.nextToken();
  String jwtSigEnc = st.nextToken();

  try {
    // Decode the signature we got from the server
    byte[] jwtExpectedSig = Base64Utility.decode(jwtSigEnc, true);

    // Validate the signature.
    Signature sig = Signature.getInstance(JWT_ALGORITHM);
    sig.initVerify(publicKey);
    sig.update(new String(jwtHeaderEnc + "." + jwtClaimsEnc).getBytes());
    assertTrue("JWT expected and actual signatures don't match", sig.verify(jwtExpectedSig));
  } catch (Base64Exception be) {
    Assert.fail("Exception decoding JWT signature: " + be.toString());
  } catch (Throwable t) {
    System.out.println(t.toString());
    t.printStackTrace(System.out);
    Assert.fail("Exception validating JWT signature: " + t.toString());
  }
}
 
開發者ID:OpenLiberty,項目名稱:sample-acmegifts,代碼行數:29,代碼來源:JWTVerifier.java

示例14: 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

示例15: verify

import java.security.Signature; //導入方法依賴的package包/類
private boolean verify(byte[] signature, byte[] message)
        throws GeneralSecurityException {
    if (publicKey == null) {
        throw new IllegalStateException("need to set public key with " +
                                        " OAuthConsumer.setProperty when " +
                                        "verifying RSA-SHA1 signatures.");
    }
    Signature verifier = Signature.getInstance("SHA1withRSA");
    verifier.initVerify(publicKey);
    verifier.update(message);
    return verifier.verify(signature);
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:13,代碼來源:RSA_SHA1.java


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