List of usage examples for java.security SecureRandom getInstance
public static SecureRandom getInstance(String algorithm) throws NoSuchAlgorithmException
From source file:wssec.TestWSSecurityNewSCT.java
public void testSCTKDKTSignEncrypt() { try {//from w ww . j a v a 2 s. c o m SOAPEnvelope unsignedEnvelope = message.getSOAPEnvelope(); Document doc = unsignedEnvelope.getAsDocument(); WSSecHeader secHeader = new WSSecHeader(); secHeader.insertSecurityHeader(doc); WSSecSecurityContextToken sctBuilder = new WSSecSecurityContextToken(); sctBuilder.prepare(doc, crypto); SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); byte[] tempSecret = new byte[16]; random.nextBytes(tempSecret); // Store the secret this.secrets.put(sctBuilder.getIdentifier(), tempSecret); String tokenId = sctBuilder.getSctId(); // Derived key signature WSSecDKSign sigBuilder = new WSSecDKSign(); sigBuilder.setExternalKey(tempSecret, tokenId); sigBuilder.setSignatureAlgorithm(XMLSignature.ALGO_ID_MAC_HMAC_SHA1); sigBuilder.build(doc, secHeader); // Derived key encryption WSSecDKEncrypt encrBuilder = new WSSecDKEncrypt(); encrBuilder.setSymmetricEncAlgorithm(WSConstants.AES_128); encrBuilder.setExternalKey(tempSecret, tokenId); encrBuilder.build(doc, secHeader); sctBuilder.prependSCTElementToHeader(doc, secHeader); if (LOG.isDebugEnabled()) { String outputString = org.apache.ws.security.util.XMLUtils.PrettyDocumentToString(doc); LOG.debug(outputString); } verify(doc); } catch (Exception e) { e.printStackTrace(); fail(e.getMessage()); } }
From source file:com.streamsets.datacollector.publicrestapi.TestCredentialsDeploymentResource.java
private KeyPair generateKeys() throws Exception { KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); keyGen.initialize(512, random);/*from w w w . ja v a2s .co m*/ return keyGen.generateKeyPair(); }
From source file:org.apache.ofbiz.base.crypto.HashCrypt.java
private static String getSalt() { try {/*from ww w .j a v a 2 s. c om*/ SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); byte[] salt = new byte[16]; sr.nextBytes(salt); return salt.toString(); } catch (NoSuchAlgorithmException e) { throw new GeneralRuntimeException("Error while creating salt", e); } }
From source file:org.killbill.billing.plugin.payeezy.client.PayeezyClientWrapper.java
private void setHeaders(final String payload, final AsyncHttpClient.BoundRequestBuilder builder) { builder.addHeader(APIResourceConstants.SecurityConstants.APIKEY, apiKey); builder.addHeader(APIResourceConstants.SecurityConstants.TOKEN, token); builder.addHeader(APIResourceConstants.SecurityConstants.APISECRET, secret); builder.addHeader("User-Agent", "KillBill 1.0"); builder.addHeader("Content-Type", "application/json"); final long nonce; try {/*from w ww .ja v a2 s . co m*/ final SecureRandom sha1PRNG = SecureRandom.getInstance(SHA1_PRNG); nonce = Math.abs(sha1PRNG.nextLong()); } catch (final NoSuchAlgorithmException e) { throw new RuntimeException(e); } final String nonceString = Long.toString(nonce); builder.addHeader(APIResourceConstants.SecurityConstants.NONCE, nonceString); final String timestamp = Long.toString(System.currentTimeMillis()); builder.addHeader(APIResourceConstants.SecurityConstants.TIMESTAMP, timestamp); try { builder.addHeader(APIResourceConstants.SecurityConstants.AUTHORIZE, getMacValue(nonceString, timestamp, payload)); } catch (final Exception e) { logger.warn("Unable to compute HMAC header", e); } }
From source file:org.eclipse.thym.ios.core.pbxproject.PBXProject.java
public static String generateReference() { try {//w ww .j a va2 s. c om MessageDigest md = MessageDigest.getInstance("SHA1"); SecureRandom prng = SecureRandom.getInstance("SHA1PRNG"); String randomNum = Integer.toString(prng.nextInt()); String ref = new String(Hex.encodeHex(md.digest(randomNum.getBytes()))); return ref.toUpperCase().substring(0, 24); } catch (NoSuchAlgorithmException e) { return null; } }
From source file:org.jboss.tools.aerogear.hybrid.ios.core.pbxproject.PBXProject.java
public static String generateReference() { MessageDigest md = null;/*ww w . jav a 2 s .com*/ SecureRandom prng = null; try { md = MessageDigest.getInstance("SHA1"); prng = SecureRandom.getInstance("SHA1PRNG"); } catch (NoSuchAlgorithmException e) { } String randomNum = new Integer(prng.nextInt()).toString(); String ref = new String(Hex.encodeHex(md.digest(randomNum.getBytes()))); return ref.toUpperCase().substring(0, 24); }
From source file:org.moe.document.pbxproj.ProjectFile.java
private String generateReference() { MessageDigest md;//from ww w . j a va 2 s . c om SecureRandom prng; try { md = MessageDigest.getInstance("SHA1"); prng = SecureRandom.getInstance("SHA1PRNG"); } catch (NoSuchAlgorithmException e) { throw new RuntimeException("Random generator failed", e); } String randomNum = Integer.toString(prng.nextInt()); String ref = new String(Hex.encodeHex(md.digest(randomNum.getBytes()))); return ref.toUpperCase().substring(0, 24); }
From source file:whitelabel.cloud.wsclient.WebServiceAuthenticator.java
/** * Generate a nonce of the given length using the SHA1PRNG algorithm. * The SecureRandom instance that backs this method is cached for efficiency. *//from w w w. ja va 2 s. c o m * @return a nonce of the given length * @throws WSSecurityException */ private final String generateNonceBase64(int length) { try { if (random == null) { random = SecureRandom.getInstance("SHA1PRNG"); random.setSeed(System.currentTimeMillis()); } byte[] temp = new byte[length]; random.nextBytes(temp); //return temp; return new String(Base64.encode(temp)); } catch (Exception ex) { throw new RuntimeException("Error in generating nonce of length " + length, ex); } }
From source file:utils.Hash.java
/** * Creates a psedorandom string//w w w .ja va2 s . com * @return Random String */ public static String randomString() { String result = new String(); try { byte byteArray[] = new byte[16]; SecureRandom psn1 = SecureRandom.getInstance("SHA1PRNG"); psn1.setSeed(psn1.nextLong()); psn1.nextBytes(byteArray); BigInteger bigInt = new BigInteger(byteArray); result = bigInt.toString(); log.debug("Generated String = " + result); } catch (Exception e) { log.error("Random Number Error : " + e.toString()); } return result; }
From source file:com.glaf.core.security.SecurityUtils.java
/** * //from w w w . jav a 2 s . c o m * * @param ctx * * @param cipherContent * * @param key * * @return byte[] ? * */ public static byte[] symmetryDecrypt(SecurityContext ctx, byte[] cipherContent, Key key) { try { byte[] tContent = null; Cipher cipher = Cipher.getInstance(ctx.getSymmetryAlgorithm(), ctx.getJceProvider()); SecureRandom secureRandom = SecureRandom.getInstance(ctx.getSecureRandomAlgorithm()); cipher.init(Cipher.DECRYPT_MODE, key, secureRandom); tContent = cipher.doFinal(cipherContent); return tContent; } catch (Exception ex) { throw new SecurityException(ex); } }