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


Java Signature類代碼示例

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


Signature類屬於java.security包,在下文中一共展示了Signature類的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: 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

示例3: sign

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

示例4: sign

import java.security.Signature; //導入依賴的package包/類
/**
 * Compute the raw signature value over the supplied input.
 * 
 * It is up to the caller to ensure that the specified algorithm ID is consistent with the type of signing key
 * supplied.
 * 
 * @param signingKey the private key with which to compute the signature
 * @param jcaAlgorithmID the Java JCA algorithm ID to use
 * @param input the input over which to compute the signature
 * @return the computed signature value
 * @throws SecurityException thrown if the signature computation results in an error
 */
public static byte[] sign(PrivateKey signingKey, String jcaAlgorithmID, byte[] input) throws SecurityException {
    Logger log = getLogger();
    log.debug("Computing signature over input using private key of type {} and JCA algorithm ID {}", signingKey
            .getAlgorithm(), jcaAlgorithmID);

    try {
        Signature signature = Signature.getInstance(jcaAlgorithmID);
        signature.initSign(signingKey);
        signature.update(input);
        byte[] rawSignature = signature.sign();
        log.debug("Computed signature: {}", new String(Hex.encode(rawSignature)));
        return rawSignature;
    } catch (GeneralSecurityException e) {
        log.error("Error during signature generation", e);
        throw new SecurityException("Error during signature generation", e);
    }
}
 
開發者ID:lamsfoundation,項目名稱:lams,代碼行數:30,代碼來源:SigningUtil.java

示例5: sign

import java.security.Signature; //導入依賴的package包/類
/**
 * 用私鑰對信息生成數字簽名
 * 
 * @param data
 *            加密數據
 * @param privateKey
 *            私鑰
 * 
 * @return
 * @throws Exception
 */
public static String sign(byte[] data, String privateKey) throws Exception {
	// 解密由base64編碼的私鑰
	byte[] keyBytes = decryptBASE64(privateKey);

	// 構造PKCS8EncodedKeySpec對象
	PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes);

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

	// 取私鑰匙對象
	PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);

	// 用私鑰對信息生成數字簽名
	Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
	signature.initSign(priKey);
	signature.update(data);

	return encryptBASE64(signature.sign());
}
 
開發者ID:wooui,項目名稱:springboot-training,代碼行數:32,代碼來源:RSACoder.java

示例6: doSign

import java.security.Signature; //導入依賴的package包/類
/**
 * Signs the given hash and returns the R and S components as BigIntegers
 * and put them in ECDSASignature
 *
 * @param input to sign
 * @return ECDSASignature signature that contains the R and S components
 */
public ECDSASignature doSign(byte[] input) {
    if (input.length != 32) {
        throw new IllegalArgumentException("Expected 32 byte input to ECDSA signature, not " + input.length);
    }
    // No decryption of private key required.
    if (privKey == null)
        throw new MissingPrivateKeyException();
    if (privKey instanceof BCECPrivateKey) {
        ECDSASigner signer = new ECDSASigner(new HMacDSAKCalculator(new SHA256Digest()));
        ECPrivateKeyParameters privKeyParams = new ECPrivateKeyParameters(((BCECPrivateKey) privKey).getD(), CURVE);
        signer.init(true, privKeyParams);
        BigInteger[] components = signer.generateSignature(input);
        return new ECDSASignature(components[0], components[1]).toCanonicalised();
    } else {
        try {
            final Signature ecSig = ECSignatureFactory.getRawInstance(provider);
            ecSig.initSign(privKey);
            ecSig.update(input);
            final byte[] derSignature = ecSig.sign();
            return ECDSASignature.decodeFromDER(derSignature).toCanonicalised();
        } catch (SignatureException | InvalidKeyException ex) {
            throw new RuntimeException("ECKey signing error", ex);
        }
    }
}
 
開發者ID:Aptoide,項目名稱:AppCoins-ethereumj,代碼行數:33,代碼來源:ECKey.java

示例7: 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;
    result = s.verify(sig);
    if (result == false) {
        throw new Exception("Verification 1 failed");
    }
    s.update(data);
    result = s.verify(sig);
    if (result == false) {
        throw new Exception("Verification 2 failed");
    }
    result = s.verify(sig);
    if (result == true) {
        throw new Exception("Verification 3 succeeded");
    }
}
 
開發者ID:AdoptOpenJDK,項目名稱:openjdk-jdk10,代碼行數:25,代碼來源:TestSignatures.java

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

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

示例10: sign

import java.security.Signature; //導入依賴的package包/類
/**
 * 簽名
 * 
 * @param data 待簽名數據
 * @param privateKey 私鑰
 * @return byte[] 數字簽名
 * @throws Exception
 */
public static byte[] sign(byte[] data, byte[] privateKey) throws Exception {
	// 轉換私鑰材料
	PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey);
	// 實例化密鑰工廠
	KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
	// 取私鑰匙對象
	PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
	// 實例化Signature
	Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
	// 初始化Signature
	signature.initSign(priKey);
	// 更新
	signature.update(data);
	// 簽名
	return signature.sign();
}
 
開發者ID:guokezheng,項目名稱:automat,代碼行數:25,代碼來源:RSACoder.java

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

示例12: test

import java.security.Signature; //導入依賴的package包/類
public static void test() throws Exception {
	KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA");

    keyGen.initialize(512, new SecureRandom());

    KeyPair keyPair = keyGen.generateKeyPair();
    Signature signature = Signature.getInstance("SHA1withRSA");

    signature.initSign(keyPair.getPrivate());

    byte[] message = "abc".getBytes();
    signature.update(message);

    byte[] sigBytes = signature.sign();
    signature.initVerify(keyPair.getPublic());
    signature.update(message);
    System.out.println(signature.verify(sigBytes));
	
}
 
開發者ID:atul19971,項目名稱:crypto4ora,代碼行數:20,代碼來源:Client.java

示例13: sign

import java.security.Signature; //導入依賴的package包/類
private byte[] sign(byte[] data) {
    byte[] signature;
    if (Environment.is(Environment.DEV)) {
        try {
            Signature rsa = Signature.getInstance("SHA256withRSA");
            rsa.initSign(gcsCredential.getServiceAccountPrivateKey());
            rsa.update(data);
            signature = rsa.sign();
        } catch (Exception e) {
            throw new GoogleCloudStorageException(e, "Error signing URL: %s", e.getMessage());
        }
    } else {
        AppIdentityService.SigningResult signingResult = appIdentityService.signForApp(data);
        signature = signingResult.getSignature();
    }
    return signature;
}
 
開發者ID:3wks,項目名稱:generator-thundr-gae-react,代碼行數:18,代碼來源:GoogleCloudStorageJsonApiClient.java

示例14: signBySoft256

import java.security.Signature; //導入依賴的package包/類
/**
 * @param privateKey
 * @param data
 * @return
 * @throws Exception
 */
public static byte[] signBySoft256(PrivateKey privateKey, byte[] data)
		throws Exception {
	byte[] result = null;
	Signature st = Signature.getInstance(BC_PROV_ALGORITHM_SHA256RSA, "BC");
	st.initSign(privateKey);
	st.update(data);
	result = st.sign();
	return result;
}
 
開發者ID:Javen205,項目名稱:IJPay,代碼行數:16,代碼來源:SecureUtil.java

示例15: sign

import java.security.Signature; //導入依賴的package包/類
/**
 * if has performance problem ,change Signature to ThreadLocal instance 
 */
public static String sign(String content, PrivateKey privateKey)
    throws NoSuchAlgorithmException, InvalidKeySpecException, SignatureException, InvalidKeyException {
  Signature signature = Signature.getInstance(SIGN_ALG);
  signature.initSign(privateKey);
  signature.update(content.getBytes());
  byte[] signByte = signature.sign();
  return encoder.encodeToString(signByte);
}
 
開發者ID:apache,項目名稱:incubator-servicecomb-java-chassis,代碼行數:12,代碼來源:RSAUtils.java


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