List of usage examples for java.security Signature initSign
public final void initSign(PrivateKey privateKey) throws InvalidKeyException
From source file:cn.usually.common.pay.union.sdk.SecureUtil.java
/** * ??/*from w w w .j a v a2 s . co 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:acp.sdk.SecureUtil.java
/** * ??//from ww w.j av a2 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, "BC"); st.initSign(privateKey); st.update(data); result = st.sign(); return result; }
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); signature.update(signingInput);// www. ja va2 s . c o m 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); signature.update(signingInput);/* w w w . j a v a 2s.c o m*/ return signature.sign(); }
From source file:org.xdi.oxauth.model.util.JwtUtil.java
public static byte[] getSignatureRS512(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("SHA512withRSA", "BC"); signature.initSign(privateKey); signature.update(signingInput);/*w ww.j a v a 2s . c o m*/ return signature.sign(); }
From source file:org.xdi.oxauth.model.util.JwtUtil.java
public static byte[] getSignatureES256(byte[] signingInput, ECDSAPrivateKey ecdsaPrivateKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException {/* w w w. jav a 2 s . co m*/ ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("P-256"); ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(ecdsaPrivateKey.getD(), ecSpec); KeyFactory keyFactory = KeyFactory.getInstance("ECDSA", "BC"); PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec); Signature signature = Signature.getInstance("SHA256WITHECDSA", "BC"); signature.initSign(privateKey); signature.update(signingInput); return signature.sign(); }
From source file:org.xdi.oxauth.model.util.JwtUtil.java
public static byte[] getSignatureES384(byte[] signingInput, ECDSAPrivateKey ecdsaPrivateKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException {//from ww w . j a v a 2 s . co m ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("P-384"); ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(ecdsaPrivateKey.getD(), ecSpec); KeyFactory keyFactory = KeyFactory.getInstance("ECDSA", "BC"); PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec); Signature signature = Signature.getInstance("SHA384WITHECDSA", "BC"); signature.initSign(privateKey); signature.update(signingInput); return signature.sign(); }
From source file:org.xdi.oxauth.model.util.JwtUtil.java
public static byte[] getSignatureES512(byte[] signingInput, ECDSAPrivateKey ecdsaPrivateKey) throws NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException, InvalidKeyException, SignatureException {/*w ww . java 2 s. c o m*/ ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("P-521"); ECPrivateKeySpec privateKeySpec = new ECPrivateKeySpec(ecdsaPrivateKey.getD(), ecSpec); KeyFactory keyFactory = KeyFactory.getInstance("ECDSA", "BC"); PrivateKey privateKey = keyFactory.generatePrivate(privateKeySpec); Signature signature = Signature.getInstance("SHA512WITHECDSA", "BC"); signature.initSign(privateKey); signature.update(signingInput); return signature.sign(); }
From source file:org.dasein.cloud.google.GoogleMethod.java
static @Nonnull String getToken(@Nonnull String iss, @Nonnull String p12File) throws CloudException { if (logger.isDebugEnabled()) { logger.debug("iss: " + iss); logger.debug("p12File: " + p12File); }//w w w . j a v a 2 s.c om String header = "{\"alg\":\"RS256\",\"typ\":\"JWT\"}"; StringBuffer token = new StringBuffer(); try { token.append(Base64.encodeBase64URLSafeString(header.getBytes("UTF-8"))); token.append("."); String scope = "https://www.googleapis.com/auth/compute"; String aud = "https://accounts.google.com/o/oauth2/token"; String expiry = Long.toString((System.currentTimeMillis() / 1000) + 3600); String startTime = Long.toString((System.currentTimeMillis() / 1000)); String payload = "{\"iss\": \"" + iss + "\", \"scope\": \"" + scope + "\", \"aud\": \"" + aud + "\", \"exp\": \"" + expiry + "\", \"iat\": \"" + startTime + "\"}"; token.append(Base64.encodeBase64URLSafeString(payload.getBytes("UTF-8"))); // TODO: the password is hardcoded. This has to be read from the ctx or from the environment variable char[] password = "notasecret".toCharArray(); FileInputStream iStream = new FileInputStream(new File(p12File)); KeyStore store = KeyStore.getInstance("PKCS12"); try { store.load(iStream, password); } finally { try { iStream.close(); } catch (IOException e) { e.printStackTrace(); logger.error("Could not read the keystore file"); throw new CloudException(e); } } String alias = ""; Enumeration<String> aliases = store.aliases(); while (aliases.hasMoreElements()) { String keyStoreAlias = aliases.nextElement().toString(); if (store.isKeyEntry(keyStoreAlias)) { alias = keyStoreAlias; break; } } PrivateKey privateKey = (PrivateKey) store.getKey(alias, password); Signature shaSignature = Signature.getInstance("SHA256withRSA"); shaSignature.initSign(privateKey); shaSignature.update(token.toString().getBytes("UTF-8")); String signedToken = Base64.encodeBase64URLSafeString(shaSignature.sign()); //Separate with a period token.append("."); //Add the encoded signature token.append(signedToken); return token.toString(); } catch (Exception e) { e.printStackTrace(); logger.error("Could not sign the payload with the private key"); throw new CloudException(e); } }
From source file:ru.codeinside.gws.crypto.cryptopro.Pkcs7Test.java
@Test public void testEncoding() throws Exception { CryptoProvider.loadCertificate();// ww w . j a va 2 s.com byte[] content = IOUtils .toByteArray(R.getRequiredResourceStream("rr/req_cb8cff92-c788-46cf-a97f-f5c6f0170be5.xml")); final Signature signature = Signature.getInstance("GOST3411withGOST3410EL"); signature.initSign(CryptoProvider.privateKey); signature.update(content); byte[] sign = signature.sign(); byte[] pkcs7 = SunPkcs7 .toPkcs7(new ru.codeinside.gws.api.Signature(CryptoProvider.cert, null, sign, false)); ru.codeinside.gws.api.Signature sig2 = SunPkcs7.fromPkcs7(pkcs7); assertEquals(CryptoProvider.cert, sig2.certificate); assertArrayEquals(sign, sig2.sign); assertTrue(R.provider.validate(sig2, null, content)); }