Example usage for java.security Signature update

List of usage examples for java.security Signature update

Introduction

In this page you can find the example usage for java.security Signature update.

Prototype

public final void update(ByteBuffer data) throws SignatureException 

Source Link

Document

Updates the data to be signed or verified using the specified ByteBuffer.

Usage

From source file:cn.usually.common.pay.union.sdk.SecureUtil.java

/**
 * ??/*from ww w.  ja  v  a 2 s  . c o m*/
 * 
 * @param privateKey
 *            ?
 * @param data
 *            ???
 * @param signMethod
 *            ??
 * @return 
 * @throws Exception
 */
public static byte[] signBySoft(PrivateKey privateKey, byte[] data) throws Exception {
    byte[] result = null;
    Signature st = Signature.getInstance(BC_PROV_ALGORITHM_SHA1RSA);
    st.initSign(privateKey);
    st.update(data);
    result = st.sign();
    return result;
}

From source file:cn.usually.common.pay.union.sdk.SecureUtil.java

/**
 * ???/* w  w  w  . j  a v a2s .c o m*/
 * 
 * @param publicKey
 *            
 * @param signData
 *            ???
 * @param srcData
 *            ?
 * @param validateMethod
 *            ??.
 * @return
 * @throws Exception
 */
public static boolean validateSignBySoft(PublicKey publicKey, byte[] signData, byte[] srcData)
        throws Exception {
    Signature st = Signature.getInstance(BC_PROV_ALGORITHM_SHA1RSA);
    st.initVerify(publicKey);
    st.update(srcData);
    return st.verify(signData);
}

From source file:acp.sdk.SecureUtil.java

/**
 * ??/*from  www .ja  v  a2 s  .  c om*/
 * 
 * @param privateKey
 *            ?
 * @param data
 *            ???
 * @param signMethod
 *            ??
 * @return 
 * @throws Exception
 */
public static byte[] signBySoft(PrivateKey privateKey, byte[] data) throws Exception {
    byte[] result = null;
    Signature st = Signature.getInstance(BC_PROV_ALGORITHM_SHA1RSA, "BC");
    st.initSign(privateKey);
    st.update(data);
    result = st.sign();
    return result;
}

From source file:acp.sdk.SecureUtil.java

/**
 * ???/*from ww  w.jav a  2  s.c  o  m*/
 * 
 * @param publicKey
 *            
 * @param signData
 *            ???
 * @param srcData
 *            ?
 * @param validateMethod
 *            ??.
 * @return
 * @throws Exception
 */
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);
}

From source file:nl.knmi.adaguc.services.oauth2.OAuth2Handler.java

/**
 * RSASSA-PKCS1-V1_5-VERIFY ((n, e), M, S) using SHA-256
 * /*w  w w .j  a va 2 s. c o m*/
 * @param modulus_n
 * @param exponent_e
 * @param signinInput_M
 * @param signature_S
 * @return
 * @throws SignatureException
 * @throws InvalidKeyException
 * @throws NoSuchAlgorithmException
 * @throws InvalidKeySpecException
 */
static boolean RSASSA_PKCS1_V1_5_VERIFY(String modulus_n, String exponent_e, String signinInput_M,
        String signature_S)
        throws SignatureException, InvalidKeyException, NoSuchAlgorithmException, InvalidKeySpecException {
    Debug.println("Starting verification");
    /* RSA SHA-256 RSASSA-PKCS1-V1_5-VERIFY */
    // Modulus (n from https://www.googleapis.com/oauth2/v2/certs)
    String n = modulus_n;
    // Exponent (e from https://www.googleapis.com/oauth2/v2/certs)
    String e = exponent_e;
    // The JWT Signing Input (JWT Header and JWT Payload concatenated with
    // ".")
    byte[] M = signinInput_M.getBytes();
    // Signature (JWT Crypto)
    byte[] S = Base64.decodeBase64(signature_S);

    byte[] modulusBytes = Base64.decodeBase64(n);
    byte[] exponentBytes = Base64.decodeBase64(e);
    BigInteger modulusInteger = new BigInteger(1, modulusBytes);
    BigInteger exponentInteger = new BigInteger(1, exponentBytes);

    RSAPublicKeySpec rsaPubKey = new RSAPublicKeySpec(modulusInteger, exponentInteger);
    KeyFactory fact = KeyFactory.getInstance("RSA");
    PublicKey pubKey = fact.generatePublic(rsaPubKey);
    Signature signature = Signature.getInstance("SHA256withRSA");
    signature.initVerify(pubKey);
    signature.update(M);
    boolean isVerified = signature.verify(S);
    Debug.println("Verify result [" + isVerified + "]");
    return isVerified;
}

From source file:org.xdi.oxauth.model.util.JwtUtil.java

public static boolean verifySignatureES256(byte[] signingInput, byte[] sigBytes, X509Certificate cert)
        throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
    PublicKey publicKey = cert.getPublicKey();

    Signature signature = Signature.getInstance("SHA256WITHECDSA", "BC");
    signature.initVerify(publicKey);/*from ww  w  .ja  v a  2s . c  o  m*/
    signature.update(signingInput);
    return signature.verify(sigBytes);
}

From source file:org.xdi.oxauth.model.util.JwtUtil.java

public static boolean verifySignatureES384(byte[] signingInput, byte[] sigBytes, X509Certificate cert)
        throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
    PublicKey publicKey = cert.getPublicKey();

    Signature signature = Signature.getInstance("SHA384WITHECDSA", "BC");
    signature.initVerify(publicKey);/* w w w .j  a v a  2  s  . c o  m*/
    signature.update(signingInput);
    return signature.verify(sigBytes);
}

From source file:org.xdi.oxauth.model.util.JwtUtil.java

public static boolean verifySignatureES512(byte[] signingInput, byte[] sigBytes, X509Certificate cert)
        throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeyException, SignatureException {
    PublicKey publicKey = cert.getPublicKey();

    Signature signature = Signature.getInstance("SHA512WITHECDSA", "BC");
    signature.initVerify(publicKey);/*from   w ww  .jav a2s.  c om*/
    signature.update(signingInput);
    return signature.verify(sigBytes);
}

From source file:org.xdi.oxauth.model.util.JwtUtil.java

public static byte[] getSignatureRS256(byte[] signingInput, RSAPrivateKey rsaPrivateKey)
        throws SignatureException, InvalidKeyException, NoSuchProviderException, InvalidKeySpecException,
        NoSuchAlgorithmException {
    RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(rsaPrivateKey.getModulus(),
            rsaPrivateKey.getPrivateExponent());

    KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
    PrivateKey privateKey = keyFactory.generatePrivate(rsaPrivateKeySpec);

    Signature signature = Signature.getInstance("SHA256withRSA", "BC");
    signature.initSign(privateKey);//w w w . ja  va 2s. co m
    signature.update(signingInput);

    return signature.sign();
}

From source file:org.xdi.oxauth.model.util.JwtUtil.java

public static byte[] getSignatureRS384(byte[] signingInput, RSAPrivateKey rsaPrivateKey)
        throws SignatureException, InvalidKeyException, NoSuchProviderException, InvalidKeySpecException,
        NoSuchAlgorithmException {
    RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(rsaPrivateKey.getModulus(),
            rsaPrivateKey.getPrivateExponent());

    KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC");
    PrivateKey privateKey = keyFactory.generatePrivate(rsaPrivateKeySpec);

    Signature signature = Signature.getInstance("SHA384withRSA", "BC");
    signature.initSign(privateKey);//  w w  w . j av  a 2  s  . c  om
    signature.update(signingInput);

    return signature.sign();
}