List of usage examples for java.security NoSuchAlgorithmException getMessage
public String getMessage()
From source file:com.appfirst.communication.AFHttpClient.java
public DefaultHttpClient getAFHttpClient() { try {/*from w ww . j a v a 2 s. co m*/ KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType()); try { trustStore.load(null, null); } catch (CertificateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } SSLSocketFactory sf = new AFSSLSocketFactory(trustStore); sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.UTF_8); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); registry.register(new Scheme("https", sf, 443)); ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry); return new DefaultHttpClient(ccm, params); } catch (NoSuchAlgorithmException nsae) { Log.e(TAG, nsae.getMessage()); return new DefaultHttpClient(); } catch (KeyManagementException kme) { Log.e(TAG, kme.getMessage()); return new DefaultHttpClient(); } catch (KeyStoreException kse) { Log.e(TAG, kse.getMessage()); return new DefaultHttpClient(); } catch (UnrecoverableKeyException uke) { Log.e(TAG, uke.getMessage()); return new DefaultHttpClient(); } }
From source file:org.alfresco.extension.countersign.action.executer.ContentSignatureActionExecuter.java
@Override protected void executeImpl(Action ruleAction, NodeRef actionedUponNodeRef) { NodeService nodeService = serviceRegistry.getNodeService(); ContentService contentService = serviceRegistry.getContentService(); byte[] sigBytes; if (nodeService.exists(actionedUponNodeRef) == false) { return;/*from w w w . ja va2s.c o m*/ } String location = (String) ruleAction.getParameterValue(PARAM_LOCATION); String geolocation = (String) ruleAction.getParameterValue(PARAM_GEOLOCATION); String reason = (String) ruleAction.getParameterValue(PARAM_REASON); String keyPassword = (String) ruleAction.getParameterValue(PARAM_KEY_PASSWORD); // get a hash of the document InputStream contentStream = contentService.getReader(actionedUponNodeRef, ContentModel.PROP_CONTENT) .getContentInputStream(); try { // get the user's private key String user = AuthenticationUtil.getRunAsUser(); SignatureProvider signatureProvider = signatureProviderFactory.getSignatureProvider(user); KeyStore keystore = signatureProvider.getUserKeyStore(keyPassword); PrivateKey key = (PrivateKey) keystore.getKey(alias, keyPassword.toCharArray()); // compute the document hash byte[] hash = signatureProvider.computeHash(contentStream); // sign the hash sigBytes = signatureProvider.signHash(hash, keyPassword); // create a "signature" node and associate it with the signed doc NodeRef sig = addSignatureNodeAssociation(actionedUponNodeRef, location, reason, "none", new java.util.Date(), geolocation, -1, "none"); // save the signature ContentWriter writer = contentService.getWriter(sig, ContentModel.PROP_CONTENT, true); writer.putContent(new ByteArrayInputStream(sigBytes)); // also save the expected hash in the signature nodeService.setProperty(sig, CounterSignSignatureModel.PROP_DOCHASH, new String(hash)); } catch (UnrecoverableKeyException uke) { throw new AlfrescoRuntimeException(uke.getMessage()); } catch (KeyStoreException kse) { throw new AlfrescoRuntimeException(kse.getMessage()); } catch (NoSuchAlgorithmException nsae) { throw new AlfrescoRuntimeException(nsae.getMessage()); } catch (Exception e) { throw new AlfrescoRuntimeException(e.getMessage()); } }
From source file:org.kopi.ebics.xml.InitializationRequestElement.java
/** * Returns the digest value of the authenticated XML portions. * @return the digest value.//from www. j a v a 2 s. c om * @throws EbicsException Failed to retrieve the digest value. */ public byte[] getDigest() throws EbicsException { addNamespaceDecl("ds", "http://www.w3.org/2000/09/xmldsig#"); try { return MessageDigest.getInstance("SHA-256", "BC").digest(Utils.canonize(toByteArray())); } catch (NoSuchAlgorithmException e) { throw new EbicsException(e.getMessage()); } catch (NoSuchProviderException e) { throw new EbicsException(e.getMessage()); } }
From source file:org.apache.hive.service.CookieSigner.java
/** * Get the signature of the input string based on SHA digest algorithm. * @param str Input token// w w w. j av a 2 s . co m * @return Signed String */ private String getSignature(String str) { try { MessageDigest md = MessageDigest.getInstance(SHA_STRING); md.update(str.getBytes()); md.update(secretBytes); byte[] digest = md.digest(); return new Base64(0).encodeToString(digest); } catch (NoSuchAlgorithmException ex) { throw new RuntimeException("Invalid SHA digest String: " + SHA_STRING + " " + ex.getMessage(), ex); } }
From source file:org.atombeat.xquery.functions.util.CopyFile.java
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException { // try to copy try {/*from w w w. ja v a 2 s.c om*/ String from = args[0].getStringValue(); String to = args[1].getStringValue(); File fromFile = new File(from); File toFile = new File(to); InputStream in = new FileInputStream(fromFile); MessageDigest md5 = MessageDigest.getInstance("MD5"); in = new DigestInputStream(in, md5); OutputStream out = new FileOutputStream(toFile); Stream.copy(in, out); String signature = new BigInteger(1, md5.digest()).toString(16); return new StringValue(signature); } catch (IOException ioe) { throw new XPathException(this, "An IO exception ocurred: " + ioe.getMessage(), ioe); } catch (NoSuchAlgorithmException e) { throw new XPathException(this, "A message digest exception ocurred: " + e.getMessage(), e); } }
From source file:com.axelor.apps.account.ebics.xml.InitializationRequestElement.java
/** * Returns the digest value of the authenticated XML portions. * @return the digest value.// w w w. j a va 2 s. c o m * @throws EbicsException Failed to retrieve the digest value. */ public byte[] getDigest() throws AxelorException { addNamespaceDecl("ds", "http://www.w3.org/2000/09/xmldsig#"); try { return MessageDigest.getInstance("SHA-256", "BC").digest(EbicsUtils.canonize(toByteArray())); } catch (NoSuchAlgorithmException e) { throw new AxelorException(e.getMessage(), IException.CONFIGURATION_ERROR); } catch (NoSuchProviderException e) { throw new AxelorException(e.getMessage(), IException.CONFIGURATION_ERROR); } }
From source file:org.ejbca.util.keystore.KeyTools.java
/** Testing a key pair to verify that it is possible to first sign and then verify with it. * // ww w . j ava 2s.co m * @param priv private key to sign a string with * @param pub public key to verify the signature with * @param provider A provider used for signing with the private key, or null if "BC" should be used. * * @throws InvalidKeyException if the public key can not be used to verify a string signed by the private key, because the key is wrong or the signature operation fails for other reasons such as a NoSuchAlgorithmException or SignatureException. * @throws NoSuchProviderException if the provider is not installed. */ public static void testKey(final PrivateKey priv, final PublicKey pub, final String provider) throws InvalidKeyException, NoSuchProviderException { final byte input[] = "Lillan gick pa vagen ut, motte dar en katt...".getBytes(); final byte signBV[]; final String testSigAlg; { final Iterator<String> i = AlgorithmTools.getSignatureAlgorithms(pub).iterator(); final String tmp = i.hasNext() ? i.next() : null; testSigAlg = tmp != null ? tmp : "SHA1WithRSA"; } if (log.isDebugEnabled()) { log.debug("Testing keys with algorithm: " + pub.getAlgorithm()); log.debug("testSigAlg: " + testSigAlg); log.debug("provider: " + provider); log.trace("privateKey: " + priv); log.trace("privateKey class: " + priv.getClass().getName()); log.trace("publicKey: " + pub); log.trace("publicKey class: " + pub.getClass().getName()); } try { { final Provider prov = Security.getProvider(provider != null ? provider : "BC"); final Signature signature = Signature.getInstance(testSigAlg, prov); signature.initSign(priv); signature.update(input); signBV = signature.sign(); if (signBV == null) { throw new InvalidKeyException("Result from signing is null."); } if (log.isDebugEnabled()) { log.trace("Created signature of size: " + signBV.length); log.trace("Created signature: " + new String(Hex.encode(signBV))); } } { final Signature signature = Signature.getInstance(testSigAlg, "BC"); signature.initVerify(pub); signature.update(input); if (!signature.verify(signBV)) { throw new InvalidKeyException("Not possible to sign and then verify with key pair."); } } } catch (NoSuchAlgorithmException e) { throw new InvalidKeyException("Exception testing key: " + e.getMessage(), e); } catch (SignatureException e) { throw new InvalidKeyException("Exception testing key: " + e.getMessage(), e); } }
From source file:net.shopxx.plugin.yeepayPayment.YeepayPaymentPlugin.java
private String hmacDigest(String value, String key) { try {//from w ww. j ava2 s . c o m Mac mac = Mac.getInstance("HmacMD5"); mac.init(new SecretKeySpec(key.getBytes("UTF-8"), "HmacMD5")); byte[] bytes = mac.doFinal(value.getBytes("UTF-8")); StringBuilder digest = new StringBuilder(); for (int i = 0; i < bytes.length; i++) { String hex = Integer.toHexString(0xFF & bytes[i]); if (hex.length() == 1) { digest.append("0"); } digest.append(hex); } return digest.toString(); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e.getMessage(), e); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e.getMessage(), e); } catch (InvalidKeyException e) { throw new RuntimeException(e.getMessage(), e); } }
From source file:org.commonjava.maven.galley.io.checksum.AbstractChecksumGenerator.java
protected AbstractChecksumGenerator(final Transfer transfer, final String checksumExtension, final ContentDigest type, final boolean writeChecksumFile) throws IOException { this.checksumExtension = checksumExtension; digestType = type;/*from w ww. j a va 2s . c o m*/ this.writeChecksumFile = writeChecksumFile; try { digester = MessageDigest.getInstance(digestType.digestName()); } catch (final NoSuchAlgorithmException e) { throw new IOException("Cannot get MessageDigest for checksum type: '" + type + "': " + e.getMessage(), e); } if (writeChecksumFile) { logger.debug("Getting checksum transfer for: {}", transfer); this.checksumTransfer = getChecksumFile(transfer); logger.debug("Locking checksum file: {}", checksumTransfer); this.checksumTransfer.lockWrite(); } else { this.checksumTransfer = null; } }
From source file:tds.itemrenderer.security.Encryption.java
/** * initializes ciphers and adds jce provider if provided * * @throws TDS.Shared.Security.TDSEncryptionException *///from w ww. j a v a2 s. co m @PostConstruct protected void init() { if (encryptionKey == null || StringUtils.isBlank(encryptionKey) || encryptionKey.length() < MINIMUM_KEY_LENGTH) { throw new TDSEncryptionException( String.format("Number of characters for key must be greater than %s", MINIMUM_KEY_LENGTH)); } try { SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance(PBE_KEY_ALGORITHM); KeySpec keySpec = new PBEKeySpec(encryptionKey.toCharArray(), PBE_SALT, PBE_NUM_ITERATIONS, PBE_KEY_LENGTH); SecretKey secretKeyTemp; secretKeyTemp = secretKeyFactory.generateSecret(keySpec); secretKey = new SecretKeySpec(secretKeyTemp.getEncoded(), CIPHER_ALGORITHM); encryptCipher = Cipher.getInstance(TRANSFORMATION); decryptCipher = Cipher.getInstance(TRANSFORMATION); encryptCipher.init(Cipher.ENCRYPT_MODE, secretKey); } catch (NoSuchAlgorithmException e) { log.error("Encyption.initCipher: " + e.getMessage(), e); throw new TDSEncryptionException("Algorithm is not available"); } catch (InvalidKeySpecException e) { log.error("Encyption.initCipher: " + e.getMessage(), e); throw new TDSEncryptionException("Key specification is not valid"); } catch (NoSuchPaddingException e) { log.error("Encyption.initCipher: " + e.getMessage(), e); throw new TDSEncryptionException("Padding is not valid"); } catch (InvalidKeyException e) { log.error("Encyption.initCipher: " + e.getMessage(), e); throw new TDSEncryptionException("Key is not valid"); } }