From 3cd9ee863cc00906578345c2869bb307930ef097 Mon Sep 17 00:00:00 2001 From: felmer <felmer> Date: Mon, 28 Jan 2008 14:06:50 +0000 Subject: [PATCH] password generator added SVN: 3817 --- .../common/utilities/PasswordGenerator.java | 123 ++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 common/source/java/ch/systemsx/cisd/common/utilities/PasswordGenerator.java diff --git a/common/source/java/ch/systemsx/cisd/common/utilities/PasswordGenerator.java b/common/source/java/ch/systemsx/cisd/common/utilities/PasswordGenerator.java new file mode 100644 index 00000000000..77c36ac8026 --- /dev/null +++ b/common/source/java/ch/systemsx/cisd/common/utilities/PasswordGenerator.java @@ -0,0 +1,123 @@ +/* + * Copyright 2008 ETH Zuerich, CISD + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.systemsx.cisd.common.utilities; + +import java.util.Random; + +/** + * Generator for human pronounceable passwords + * + * @author Franz-Josef Elmer + */ +public class PasswordGenerator +{ + private enum State + { + DIGIT() + { + @Override + State nextState(Random random) + { + return random.nextBoolean() ? CONSONANT : VOWEL; + } + + @Override + String getCharacters() + { + return "0123456789"; + } + }, + + CONSONANT() + { + @Override + State nextState(Random random) + { + return random.nextBoolean() ? DIGIT : VOWEL; + } + + @Override + String getCharacters() + { + return "bdcfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXZ"; + } + + }, + + VOWEL() + { + @Override + State nextState(Random random) + { + return random.nextBoolean() ? CONSONANT : DIGIT; + } + + @Override + String getCharacters() + { + return "aeiouyAEIOUY"; + } + }; + + State nextState(Random random) + { + return this; + } + + void appendCharacterTo(StringBuilder builder, Random random) + { + String characters = getCharacters(); + builder.append(characters.charAt(random.nextInt(characters.length()))); + } + + abstract String getCharacters(); + } + + private final Random random; + + /** + * Creates an instance based on the default constructor of {@link Random}. + */ + public PasswordGenerator() + { + this(new Random()); + } + + /** + * Creates an instance which uses the specified random number generator. + */ + public PasswordGenerator(Random random) + { + assert random != null : "Unspecified random number generator."; + this.random = random; + } + + /** + * Creates a password of specified length. + */ + public String generatePassword(int length) + { + State state = State.DIGIT; + StringBuilder builder = new StringBuilder(); + for (int i = 0; i < length; i++) + { + state = state.nextState(random); + state.appendCharacterTo(builder, random); + } + return builder.toString(); + } +} -- GitLab