List of usage examples for java.security Signature initSign
public final void initSign(PrivateKey privateKey) throws InvalidKeyException
From source file:org.wso2.carbon.device.mgt.iot.virtualfirealarm.service.impl.util.SecurityManager.java
public static String signMessage(String encryptedData, PrivateKey signatureKey) throws VirtualFireAlarmException { Signature signature; String signedEncodedString;// ww w . ja v a 2s. c o m try { signature = Signature.getInstance(SIGNATURE_ALG); signature.initSign(signatureKey); signature.update(Base64.decodeBase64(encryptedData)); byte[] signatureBytes = signature.sign(); signedEncodedString = Base64.encodeBase64String(signatureBytes); } catch (NoSuchAlgorithmException e) { String errorMsg = "Algorithm not found exception occurred for Signature instance of [" + SIGNATURE_ALG + "]"; log.error(errorMsg); throw new VirtualFireAlarmException(errorMsg, e); } catch (SignatureException e) { String errorMsg = "Signature exception occurred for Signature instance of [" + SIGNATURE_ALG + "]"; log.error(errorMsg); throw new VirtualFireAlarmException(errorMsg, e); } catch (InvalidKeyException e) { String errorMsg = "InvalidKey exception occurred for signatureKey \n[\n" + signatureKey + "\n]\n"; log.error(errorMsg); throw new VirtualFireAlarmException(errorMsg, e); } return signedEncodedString; }
From source file:org.wso2.carbon.device.mgt.iot.virtualfirealarm.plugin.impl.util.VirtualFirealarmSecurityManager.java
public static String signMessage(String encryptedData, PrivateKey signatureKey) throws VirtualFirealarmDeviceMgtPluginException { Signature signature; String signedEncodedString;/*from w ww .j a v a 2s. c om*/ try { signature = Signature.getInstance(SIGNATURE_ALG); signature.initSign(signatureKey); signature.update(Base64.decodeBase64(encryptedData)); byte[] signatureBytes = signature.sign(); signedEncodedString = Base64.encodeBase64String(signatureBytes); } catch (NoSuchAlgorithmException e) { String errorMsg = "Algorithm not found exception occurred for Signature instance of [" + SIGNATURE_ALG + "]"; log.error(errorMsg); throw new VirtualFirealarmDeviceMgtPluginException(errorMsg, e); } catch (SignatureException e) { String errorMsg = "Signature exception occurred for Signature instance of [" + SIGNATURE_ALG + "]"; log.error(errorMsg); throw new VirtualFirealarmDeviceMgtPluginException(errorMsg, e); } catch (InvalidKeyException e) { String errorMsg = "InvalidKey exception occurred for signatureKey \n[\n" + signatureKey + "\n]\n"; log.error(errorMsg); throw new VirtualFirealarmDeviceMgtPluginException(errorMsg, e); } return signedEncodedString; }
From source file:com.eucalyptus.blockstorage.HttpTransfer.java
/** * Calculates and sets the Authorization header value for the request using the EucaRSA-V2 signing algorithm * Algorithm Overview:/*from ww w.j a va 2s. c o m*/ * * 1. Generate the canonical Request * a.) CanonicalRequest = * HTTPRequestMethod + '\n' + * CanonicalURI + '\n' + * CanonicalQueryString + '\n' + * CanonicalHeaders + '\n' + * SignedHeaders * b.) Where CanonicalURI = * c.) Where CanonicalQueryString = * d.) Where CanonicalHeaders = sorted (by lowercased header name) ';' delimited list of <lowercase(headername)>:<value> items * e.) Where SignedHeaders = sorted, ';' delimited list of headers in CanonicalHeaders * * 2. Signature = RSA(privkey, SHA256(CanonicalRequest)) * * 3. Add an Authorization HTTP header to the request that contains the following strings, separated by spaces: * EUCA2-RSA-SHA256 * The lower-case hexadecimal encoding of the component's X.509 certificate's md5 fingerprint * The SignedHeaders list calculated in Task 1 * The Base64 encoding of the Signature calculated in Task 2 * * @param httpBaseRequest -- the request, the 'Authorization' header will be added to the request */ public static void signEucaInternal(HttpMethodBase httpBaseRequest) { StringBuilder canonicalRequest = new StringBuilder(); String canonicalURI = null; String verb = httpBaseRequest.getName(); canonicalURI = httpBaseRequest.getPath(); String canonicalQuery = calcCanonicalQuery(httpBaseRequest); String[] processedHeaders = getCanonicalAndSignedHeaders(httpBaseRequest); String canonicalHeaders = processedHeaders[0]; String signedHeaders = processedHeaders[1]; canonicalRequest.append(verb).append('\n'); canonicalRequest.append(canonicalURI).append('\n'); canonicalRequest.append(canonicalQuery).append('\n'); canonicalRequest.append(canonicalHeaders).append('\n'); canonicalRequest.append(signedHeaders); StringBuilder authHeader = new StringBuilder(EUCA2_AUTH_ID); String signature = null; String fingerprint = null; try { Credentials ccCreds = SystemCredentials.lookup(Storage.class); PrivateKey ccPrivateKey = ccCreds.getPrivateKey(); fingerprint = ccCreds.getCertFingerprint(); Signature sign = Signature.getInstance("SHA256withRSA"); sign.initSign(ccPrivateKey); LOG.debug("Signing canonical request: " + canonicalRequest.toString()); sign.update(canonicalRequest.toString().getBytes()); byte[] sig = sign.sign(); signature = new String(Base64.encode(sig)); } catch (Exception ex) { LOG.error("Signing error while signing request", ex); } authHeader.append(" ").append(fingerprint.toLowerCase()).append(" ").append(signedHeaders.toString()) .append(" ").append(signature); httpBaseRequest.addRequestHeader(EUCA2_AUTH_HEADER_NAME, authHeader.toString()); }
From source file:org.apache.abdera2.common.security.HashHelper.java
public static String sig(PrivateKey key, String alg, byte[] mat) { try {//w w w. j av a2 s . c om Signature sig = Signature.getInstance(alg); sig.initSign((PrivateKey) key); sig.update(mat); byte[] dat = sig.sign(); return Base64.encodeBase64URLSafeString(dat); } catch (Throwable t) { throw ExceptionHelper.propogate(t); } }
From source file:com.vmware.identity.rest.core.util.RequestSigner.java
/** * Signs a string using the private key and SHA 256 with RSA signing algorithm, and * returns it as a hex-encoded string./*from ww w.jav a 2s . com*/ * * @param signingString the string to sign. * @param privateKey the private key to sign the string with. * @return the signed string in a hex-encoded format. * @throws InvalidKeyException if the key is invalid. * @throws SignatureException if the signature algorithm is unable to process the input * data provided. */ public static String sign(String signingString, PrivateKey privateKey) throws InvalidKeyException, SignatureException { byte[] bytes = signingString.getBytes(StandardCharsets.UTF_8); Signature sig; try { sig = Signature.getInstance(SHA256_WITH_RSA); } catch (NoSuchAlgorithmException e) { throw new IllegalArgumentException("An error occurred while getting the signature algorithm", e); } sig.initSign(privateKey); sig.update(bytes); return Hex.encodeHexString(sig.sign()); }
From source file:cn.mrdear.pay.util.RSAUtils.java
/** * ???//from ww w . j a v a 2 s.c o m * * @param algorithm * ?? * @param privateKey * ? * @param data * ? * @return ?? */ public static byte[] sign(String algorithm, PrivateKey privateKey, byte[] data) { Assert.isNotEmpty(algorithm); Assert.notNull(privateKey); Assert.notNull(data); try { Signature signature = Signature.getInstance(algorithm, PROVIDER); signature.initSign(privateKey); signature.update(data); return signature.sign(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e.getMessage(), e); } catch (InvalidKeyException e) { throw new RuntimeException(e.getMessage(), e); } catch (SignatureException e) { throw new RuntimeException(e.getMessage(), e); } }
From source file:com.valco.utility.FacturasUtility.java
private static byte[] getBytesCadenaFirmada(java.security.PrivateKey pk, OutputStream output) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException, UnsupportedEncodingException { Signature firma = Signature.getInstance("SHA1withRSA"); firma.initSign(pk); firma.update(output.toString().getBytes("UTF-8")); byte[] cadenaFirmada = firma.sign(); return cadenaFirmada; }
From source file:org.umit.icm.mobile.utils.RSACrypto.java
/** * Returns an RSA KeyPair generated using * {@link KeyPairGenerator#generateKeyPair()}. * /*w ww . ja va 2s . co m*/ * @return {@link KeyPair} * @see KeyPairGenerator */ public static byte[] Sign(PrivateKey privateKey, byte[] data) throws Exception { if (Constants.DEBUG_MODE) System.out.println("Signing the key inside RSACrypto#Sign"); Signature dsa = Signature.getInstance("SHA1withRSA"); dsa.initSign(privateKey); dsa.update(data); return dsa.sign(); }
From source file:com.glaf.core.security.SecurityUtils.java
/** * ?????????/*from ww w . j a v a2 s . com*/ * * @param ctx * * @param content * ?? * @param privateKey * ? * @return byte[] ??? */ public static byte[] sign(SecurityContext ctx, byte[] content, Key privateKey) { try { Signature sign = Signature.getInstance(ctx.getSignatureAlgorithm(), ctx.getJceProvider()); PrivateKey pk = (PrivateKey) privateKey; sign.initSign(pk); sign.update(content); byte[] signed = sign.sign(); return signed; } catch (Exception ex) { throw new SecurityException(ex); } }
From source file:gemlite.core.util.RSAUtils.java
/** * <p>//from w w w. j a v a 2 s .co m * ????? * </p> * * @param data * ? * @param privateKey * ?(BASE64?) * * @return * @throws Exception */ public static String sign(byte[] data, String privateKey) throws Exception { byte[] keyBytes = Base64Utils.decode(privateKey); PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(keyBytes); KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM); PrivateKey privateK = keyFactory.generatePrivate(pkcs8KeySpec); Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM); signature.initSign(privateK); signature.update(data); return Base64Utils.encode(signature.sign()); }