List of usage examples for java.security SecureRandom SecureRandom
public SecureRandom(byte[] seed)
From source file:edu.stanford.mobisocial.dungbeetle.model.Feed.java
public static int colorFor(String name) { ByteArrayOutputStream bos = new ByteArrayOutputStream(); try {// w ww . ja v a 2s .c o m bos.write(name.getBytes()); } catch (IOException e) { } SecureRandom r = new SecureRandom(bos.toByteArray()); float hsv[] = new float[] { baseHues[r.nextInt(baseHues.length)], r.nextFloat(), r.nextFloat() }; hsv[0] = hsv[0] + 20 * r.nextFloat() - 10; hsv[1] = hsv[1] * 0.2f + 0.8f; hsv[2] = hsv[2] * 0.2f + 0.8f; return Color.HSVToColor(hsv); }
From source file:hivemall.math.random.RandomNumberGeneratorFactory.java
@Nonnull public static PRNG createPRNG(@Nonnull PRNGType type, long seed) { final PRNG rng; switch (type) { case java://from www . jav a 2 s . co m rng = new JavaRandom(seed); break; case secure: rng = new JavaRandom(new SecureRandom(Primitives.toBytes(seed))); break; case smile: rng = new SmileRandom(seed); break; case smileMT: rng = new SmileRandom(new smile.math.random.MersenneTwister(Primitives.hashCode(seed))); break; case smileMT64: rng = new SmileRandom(new smile.math.random.MersenneTwister64(seed)); break; case commonsMath3MT: rng = new CommonsMathRandom(new org.apache.commons.math3.random.MersenneTwister(seed)); break; default: throw new IllegalStateException("Unexpected type: " + type); } return rng; }
From source file:RandomOrgSeededRandomGenerator.java
/** * Construct a new random.org seeded random generator. * /* w ww .java 2 s . c o m*/ */ private RandomOrgSeededRandomGenerator() { try { final URL url = new URL( "http://www.random.org/strings/?num=10&len=10&digits=on&unique=on&format=plain&rnd=new"); URLConnection connection = url.openConnection(); connection.setConnectTimeout(5000); final BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); final StringBuilder stringBuilder = new StringBuilder(); String line; while ((line = in.readLine()) != null) { stringBuilder.append(line); } in.close(); final byte[] seed = stringBuilder.toString().getBytes(); random = new SecureRandom(seed); } catch (final MalformedURLException e) { //logger.info("Default secure random is used."); random = new SecureRandom(); } catch (final IOException e) { //logger.info("Default secure random is used."); random = new SecureRandom(); } }
From source file:my.adam.smo.common.SymmetricEncryptionBox.java
@PostConstruct public void init() throws NoSuchAlgorithmException { secureRandom = new SecureRandom(key.getBytes()); try {/*from w w w . ja v a 2 s . c om*/ MessageDigest md = MessageDigest.getInstance("SHA-256"); aesKey = md.digest(key.getBytes()); } catch (NoSuchAlgorithmException e) { logger.error("error while getting digest algorithm", e); } }
From source file:de.inpiraten.jdemocrator.TAN.generator.TANGenerator.java
public void generateMasterTANs() throws IOException { //Read seed from System.in System.out.println("You will be asked to specify a random seed. Please enter a number of random"); System.out.println("characters to your liking. This will help to seed a secure random number"); System.out.println("generator which will generate the master TANs for every vote and derive the"); System.out.println("TANs. The longer your seed the stronger your master TANs."); System.out.print("\nEnter the seed for the TAN generator.\n> "); String seed = commandLineInput.readLine(); SecureRandom random = new SecureRandom( ArrayUtils.addAll(seed.getBytes(), ("" + System.currentTimeMillis()).getBytes())); //Get length of master TANs int masterTANlength = -1; while (masterTANlength < 6) { System.out.print("\nPlease enter the length of the master TANs in bytes. (Standard: 18)\n>"); int input; try {/*from www . ja v a 2 s . c om*/ input = inputInteger(); if (input > 5) { masterTANlength = input; } else if (input > 0) { System.out .println("Lengths shorter than 6 byte are too insecure. Please chose another length."); } else if (input == 0) { masterTANlength = 18; //standard option } else throw new NumberFormatException("Length of master TAN must be positive"); } catch (NumberFormatException e) { System.out.println("Invalid input. Please try again."); } } this.masterTAN = new String[this.event.numberOfElections]; byte[] rawTAN = new byte[masterTANlength]; for (int i = 0; i < this.masterTAN.length; i++) { random.nextBytes(rawTAN); this.masterTAN[i] = Base64.encodeBase64String(rawTAN); } }
From source file:Global.java
private Action<Void> getConfigSecretAction() { return new Action.Simple() { @Override/*from w w w. j a v a2 s .com*/ public Result call(Http.Context ctx) throws Throwable { if (ctx.request().method().toLowerCase().equals("post")) { Form<User> newSiteAdminUserForm = form(User.class).bindFromRequest(); if (hasError(newSiteAdminUserForm)) { return badRequest(secret.render(SiteAdmin.SITEADMIN_DEFAULT_LOGINID, newSiteAdminUserForm)); } User siteAdmin = SiteAdmin.updateDefaultSiteAdmin(newSiteAdminUserForm.get()); replaceSiteSecretKey(createSeed(siteAdmin.password)); isRestartRequired = true; return ok(restart.render()); } else { return ok(secret.render(SiteAdmin.SITEADMIN_DEFAULT_LOGINID, new Form<>(User.class))); } } private String createSeed(String basicSeed) { String seed = basicSeed; try { seed += InetAddress.getLocalHost(); } catch (Exception e) { play.Logger.warn("Failed to get localhost address", e); } return seed; } private void replaceSiteSecretKey(String seed) throws IOException { SecureRandom random = new SecureRandom(seed.getBytes()); String secret = new BigInteger(130, random).toString(32); Path path = Paths.get("conf/application.conf"); byte[] bytes = Files.readAllBytes(path); String config = new String(bytes); config = config.replace(DEFAULT_SECRET, secret); Files.write(path, config.getBytes()); } private boolean hasError(Form<User> newUserForm) { if (StringUtils.isBlank(newUserForm.field("loginId").value())) { newUserForm.reject("loginId", "user.wrongloginId.alert"); } if (!newUserForm.field("loginId").value().equals("admin")) { newUserForm.reject("loginId", "user.wrongloginId.alert"); } if (StringUtils.isBlank(newUserForm.field("password").value())) { newUserForm.reject("password", "user.wrongPassword.alert"); } if (!newUserForm.field("password").value().equals(newUserForm.field("retypedPassword").value())) { newUserForm.reject("retypedPassword", "user.confirmPassword.alert"); } if (StringUtils.isBlank(newUserForm.field("email").value())) { newUserForm.reject("email", "validation.invalidEmail"); } if (User.isEmailExist(newUserForm.field("email").value())) { newUserForm.reject("email", "user.email.duplicate"); } return newUserForm.hasErrors(); } }; }