本文整理汇总了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)));
}
}
示例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));
}
示例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 );
}
}
示例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);
}
}
示例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));
}
示例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);
}
示例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;
}
};
}
示例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);
}
示例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
}
}
示例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);
}
}
示例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");
}
}
示例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);
}
示例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());
}
}
示例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);
}
示例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);
}