|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| PasswordGenerator.java | 75% | 90% | 100% | 87.5% |
|
||||||||||||||
| 1 | // | |
| 2 | //Copyright (c) 2003, Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. | |
| 3 | //All rights reserved. | |
| 4 | // | |
| 5 | //Redistribution and use in source and binary forms, with or without modification, | |
| 6 | //are permitted provided that the following conditions are met: | |
| 7 | // | |
| 8 | //* Redistributions of source code must retain the above copyright notice, | |
| 9 | //this list of conditions and the following disclaimer. | |
| 10 | //* Redistributions in binary form must reproduce the above copyright notice, | |
| 11 | //this list of conditions and the following disclaimer in the documentation | |
| 12 | //and/or other materials provided with the distribution. | |
| 13 | //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. | |
| 14 | //nor the names of its contributors may be used to endorse or promote products | |
| 15 | //derived from this software without specific prior written permission. | |
| 16 | // | |
| 17 | //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 18 | //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 19 | //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
| 20 | //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 21 | //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 22 | //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | |
| 23 | //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
| 24 | //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 25 | //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 26 | //POSSIBILITY OF SUCH DAMAGE. | |
| 27 | // | |
| 28 | ||
| 29 | package org.objectledge.authentication; | |
| 30 | ||
| 31 | import java.util.Random; | |
| 32 | ||
| 33 | /** | |
| 34 | * Default password generator. | |
| 35 | * | |
| 36 | * @author <a href="mailto:pablo@caltha.pl">Pawel Potempski</a> | |
| 37 | */ | |
| 38 | public class PasswordGenerator | |
| 39 | { | |
| 40 | /** The pseudorandom numbers generator */ | |
| 41 | private Random randomGenerator; | |
| 42 | ||
| 43 | /** | |
| 44 | * Alphabet consisting of letters A-Z and digits 0-9. | |
| 45 | */ | |
| 46 | private static final char[] ALPHABET = { | |
| 47 | 'A','B','C','D','E','F','G','H', | |
| 48 | 'I','J','K','L','M','N','O','P', | |
| 49 | 'Q','R','S','T','U','V','W','X', | |
| 50 | 'Y','Z','1','2','3','4','5','6', | |
| 51 | '7','8','9','0','a','b','c','d', | |
| 52 | 'e','f','g','h','i','j','k','l', | |
| 53 | 'm','n','o','p','q','r','s','t', | |
| 54 | 'u','v','w','x','y','z', | |
| 55 | }; | |
| 56 | ||
| 57 | /** | |
| 58 | * Component constructor. | |
| 59 | */ | |
| 60 | 782 | public PasswordGenerator() |
| 61 | { | |
| 62 | 782 | randomGenerator = new Random(); |
| 63 | } | |
| 64 | ||
| 65 | /** | |
| 66 | * Creates a random password. | |
| 67 | */ | |
| 68 | 46 | public String createRandomPassword(int min, int max) |
| 69 | { | |
| 70 | 46 | int length = 0; |
| 71 | 46 | if (min < max) |
| 72 | { | |
| 73 | 46 | int offset = randomGenerator.nextInt(max - min + 1); |
| 74 | 46 | length = min + offset; |
| 75 | } | |
| 76 | else | |
| 77 | { | |
| 78 | 0 | length = min; |
| 79 | } | |
| 80 | 46 | StringBuilder sb = new StringBuilder(); |
| 81 | 46 | for (int i = 0; i < length; i++) |
| 82 | { | |
| 83 | 329 | sb.append(ALPHABET[randomGenerator.nextInt(ALPHABET.length)]); |
| 84 | } | |
| 85 | 46 | return sb.toString(); |
| 86 | } | |
| 87 | } |
|
||||||||||