List of usage examples for java.security Signature getInstance
public static Signature getInstance(String algorithm, Provider provider) throws NoSuchAlgorithmException
From source file:nl.arietimmerman.u2f.server.CryptoHelper.java
public static byte[] sign(PrivateKey privateKey, byte[] message) { try {/* w w w . j av a 2 s . co m*/ Signature signatureObject = Signature.getInstance("SHA256withECDSA", new BouncyCastleProvider()); signatureObject.initSign(privateKey); signatureObject.update(message); return signatureObject.sign(); } catch (InvalidKeyException | SignatureException | NoSuchAlgorithmException e) { e.printStackTrace(); return null; } }
From source file:com.POLIS.licensing.common.license.AbstractSerializationBasedLicense.java
@Override public void signLicense(PrivateKey privateSignatureKey) throws BadLicenseException, SystemStateException, OperationException { try {/* w ww . ja v a2s .co m*/ Signature instance = Signature.getInstance(signatureEncoding, provider); instance.initSign(privateSignatureKey); instance.update(getFieldsAsString().getBytes()); signature = instance.sign(); } catch (NoSuchAlgorithmException | NoSuchProviderException ex) { throw new SystemStateException("Could not sign the license. Algorithm not found", ex); } catch (InvalidKeyException | SignatureException ex) { throw new OperationException("Could not sign the license.", ex); } }
From source file:org.gluu.com.ox_push2.u2f.v2.cert.KeyPairGeneratorImpl.java
@Override public byte[] sign(byte[] signedData, PrivateKey privateKey) throws U2FException { try {/*from ww w . java 2s . c o m*/ Signature signature = Signature.getInstance("SHA256withECDSA", bouncyCastleProvider); signature.initSign(privateKey); signature.update(signedData); return signature.sign(); } catch (NoSuchAlgorithmException ex) { throw new U2FException("Error when signing", ex); } catch (SignatureException ex) { throw new U2FException("Error when signing", ex); } catch (InvalidKeyException ex) { throw new U2FException("Error when signing", ex); } }
From source file:com.alliander.osgp.oslp.OslpUtils.java
/** * Create a signature of specified message. * * @param message//from ww w . j av a 2 s . com * message bytes to sign * @param privateKey * private key to use for signing * @param signature * signature algorithm to use * @param provider * provider which supplies the signature algorithm * @return signature * @throws GeneralSecurityException * when configuration is incorrect. */ public static byte[] createSignature(final byte[] message, final PrivateKey privateKey, final String signature, final String provider) throws GeneralSecurityException { // Use fallback to plain SHA512 hash, which is encrypted with RSA // instead of real RSA signature if (signature.equalsIgnoreCase(FALLBACK_SIGNATURE)) { return createEncryptedHash(message, privateKey); } // Use real signature final Signature signatureBuilder = Signature.getInstance(signature, provider); signatureBuilder.initSign(privateKey); signatureBuilder.update(message); return signatureBuilder.sign(); }
From source file:org.xdi.oxauth.model.jws.RSASigner.java
@Override public String generateSignature(String signingInput) throws SignatureException { if (getSignatureAlgorithm() == null) { throw new SignatureException("The signature algorithm is null"); }/* ww w . jav a 2 s. co m*/ if (rsaPrivateKey == null) { throw new SignatureException("The RSA private key is null"); } if (signingInput == null) { throw new SignatureException("The signing input is null"); } try { RSAPrivateKeySpec rsaPrivateKeySpec = new RSAPrivateKeySpec(rsaPrivateKey.getModulus(), rsaPrivateKey.getPrivateExponent()); KeyFactory keyFactory = KeyFactory.getInstance("RSA", "BC"); PrivateKey privateKey = keyFactory.generatePrivate(rsaPrivateKeySpec); Signature signature = Signature.getInstance(getSignatureAlgorithm().getAlgorithm(), "BC"); signature.initSign(privateKey); signature.update(signingInput.getBytes(Util.UTF8_STRING_ENCODING)); return JwtUtil.base64urlencode(signature.sign()); } catch (InvalidKeySpecException e) { throw new SignatureException(e); } catch (InvalidKeyException e) { throw new SignatureException(e); } catch (NoSuchAlgorithmException e) { throw new SignatureException(e); } catch (NoSuchProviderException e) { throw new SignatureException(e); } catch (SignatureException e) { throw new SignatureException(e); } catch (UnsupportedEncodingException e) { throw new SignatureException(e); } catch (Exception e) { throw new SignatureException(e); } }
From source file:test.be.fedict.eid.applet.SignatureServiceImpl.java
public void postSign(byte[] signatureValue, List<X509Certificate> signingCertificateChain) { LOG.debug("postSign"); String signatureValueStr = new String(Hex.encodeHex(signatureValue)); HttpSession session = getHttpSession(); session.setAttribute("SignatureValue", signatureValueStr); session.setAttribute("SigningCertificateChain", signingCertificateChain); boolean signatureValid = false; String toBeSigned = (String) session.getAttribute("toBeSigned"); LOG.debug("to be signed: " + toBeSigned); String digestAlgo = (String) session.getAttribute("digestAlgo"); String signAlgo = digestAlgoToSignAlgo.get(digestAlgo); try {/* ww w . j a va2 s . co m*/ Signature signature = Signature.getInstance(signAlgo, BouncyCastleProvider.PROVIDER_NAME); signature.initVerify(signingCertificateChain.get(0).getPublicKey()); signature.update(toBeSigned.getBytes()); signatureValid = signature.verify(signatureValue); } catch (Exception e) { LOG.error("error validating the signature: " + e.getMessage(), e); } session.setAttribute("SignatureValid", signatureValid); }
From source file:com.POLIS.licensing.common.license.AbstractSerializationBasedLicense.java
@Override public boolean verifyLicense(PublicKey senderSignatureKey) throws BadLicenseException, SystemStateException, OperationException { if (signature == null) { throw new OperationException("Could not vertify signature. License was never signed"); }// w ww. ja v a 2s. c o m try { Signature instance = Signature.getInstance(signatureEncoding, provider); instance.initVerify(senderSignatureKey); instance.update(getFieldsAsString().getBytes()); return instance.verify(signature); } catch (NoSuchAlgorithmException | NoSuchProviderException ex) { throw new SystemStateException("Could not verify the license. Algorithm not found", ex); } catch (InvalidKeyException | SignatureException ex) { throw new OperationException("Could not verify the license.", ex); } }
From source file:com.mycompany.bankinterface.crypto.Signer.java
public String sign(String clearText) throws SignerException { Signature dsa;/*w w w . j a va 2 s . com*/ try { dsa = Signature.getInstance(SIGNER_ALGORITHM, PROVIDER); } catch (NoSuchAlgorithmException | NoSuchProviderException ex) { throw new SignerException("Could not get signing instance", ex); } try { dsa.initSign(keyPair.getPrivate()); dsa.update(clearText.getBytes()); byte[] signature = dsa.sign(); String signatureAsString = Base64.encodeBase64String(signature); return signatureAsString; } catch (InvalidKeyException | SignatureException ex) { throw new SignerException("Failed to initialise signing algorithm", ex); } }
From source file:org.cesecore.audit.audit.SigningFileOutputStream.java
/** * Generates a signature file with the same name as the export file but with .sig extension. * // w w w . j av a2s . co m * @param exportFile the exported file. * @param cryptoToken the crypto token that will be used to fetch the necessary keys. * @param signatureDetails * Set properties containing signature details like * keyAlias(EXPORT_SIGN_KEYALIAS), algorithm(EXPORT_SIGN_ALG) and * certificate( EXPORT_SIGN_CERT ). * @return the full pathname of the signature file */ public SigningFileOutputStream(final File file, final CryptoToken cryptoToken, final Map<String, Object> signatureDetails) throws FileNotFoundException, CryptoTokenOfflineException, NoSuchAlgorithmException, NoSuchProviderException, InvalidKeyException { super(file); signatureFilename = String.format("%s.sig", FilenameUtils.removeExtension(file.getAbsolutePath())); final String keyAlias = (String) signatureDetails.get(SigningFileOutputStream.EXPORT_SIGN_KEYALIAS); final PrivateKey privateKey = cryptoToken.getPrivateKey(keyAlias); final PublicKey publicKey = cryptoToken.getPublicKey(keyAlias); final String algorithm = (String) signatureDetails.get(SigningFileOutputStream.EXPORT_SIGN_ALG); signature = Signature.getInstance(algorithm, cryptoToken.getSignProviderName()); signature.initSign(privateKey); signValidate = Signature.getInstance(algorithm, cryptoToken.getSignProviderName()); final Certificate cert = (Certificate) signatureDetails.get(SigningFileOutputStream.EXPORT_SIGN_CERT); if (cert != null) { signValidate.initVerify(cert); } else { signValidate.initVerify(publicKey); } }
From source file:com.xk72.cocoafob.LicenseGenerator.java
/** * Make and return a license for the given {@link LicenseData}. * @param licenseData//w ww .j a va 2 s . com * @return * @throws LicenseGeneratorException If the generation encounters an error, usually due to invalid input. * @throws IllegalStateException If the generator is not setup correctly to make licenses. */ public String makeLicense(LicenseData licenseData) throws LicenseGeneratorException, IllegalStateException { if (!isCanMakeLicenses()) { throw new IllegalStateException( "The LicenseGenerator cannot make licenses as it was not configured with a private key"); } final String stringData = licenseData.toLicenseStringData(); try { final Signature dsa = Signature.getInstance("SHA1withDSA", "SUN"); dsa.initSign(privateKey, random); dsa.update(stringData.getBytes("UTF-8")); final byte[] signed = dsa.sign(); /* base 32 encode the signature */ String result = new Base32().encodeAsString(signed); /* replace O with 8 and I with 9 */ result = result.replace("O", "8").replace("I", "9"); /* remove padding if any. */ result = result.replace("=", ""); /* chunk with dashes */ result = split(result, 5); return result; } catch (NoSuchAlgorithmException e) { throw new LicenseGeneratorException(e); } catch (NoSuchProviderException e) { throw new LicenseGeneratorException(e); } catch (InvalidKeyException e) { throw new LicenseGeneratorException(e); } catch (SignatureException e) { throw new LicenseGeneratorException(e); } catch (UnsupportedEncodingException e) { throw new LicenseGeneratorException(e); } }