📄 passwordgenerator.java
字号:
/* * Copyright 2006 Marcel Schoffelmeer * * 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 com.s10r.util;import java.util.Random;public class PasswordGenerator{ public static final int NUMERIC = 1; public static final int UPPER_ALPHA = 2; public static final int LOWER_ALPHA = 4; public static final int SPECIAL = 8; public static final int BRACKET = 16; private static char[] NUMERIC_CHARS = new char[10]; private static char[] UPPER_ALPHA_CHARS = new char[26]; private static char[] LOWER_ALPHA_CHARS = new char[26]; private static char[] SPECIAL_CHARS = new char[16]; private static char[] BRACKET_CHARS = new char[] { '{', '}', '[', ']' }; static { for (int i = 0; i < UPPER_ALPHA_CHARS.length; i++) { UPPER_ALPHA_CHARS[i] = (char) (65 + i); LOWER_ALPHA_CHARS[i] = (char) (97 + i); } for (int i = 0; i < NUMERIC_CHARS.length; i++) { NUMERIC_CHARS[i] = (char) (48 + i); } for (int i = 0; i < SPECIAL_CHARS.length; i++) { SPECIAL_CHARS[i] = (char) (33 + i); } } private char[] charSet = new char[300]; private int charSetLength = 0; private char[] customChars; private Random RANDOM = new Random(); // Note: DEFAULT must be instantiated after the static block // to make sure the individual char sets are initialized public static final PasswordGenerator DEFAULT = new PasswordGenerator(); public PasswordGenerator() { this(NUMERIC | UPPER_ALPHA | LOWER_ALPHA); } public PasswordGenerator(int selectedCharSets, char[] customChars) { this.customChars = customChars; charSetLength = createCharSet(selectedCharSets); } public String generate(int length) { char[] newPass = new char[length]; for (int i = 0; i < length; i++) { newPass[i] = charSet[RANDOM.nextInt(charSetLength)]; } String newPassStr = new String(newPass, 0, length); return newPassStr; } public PasswordGenerator(int selectedCharSets) { this(selectedCharSets, null); } private int createCharSet(int selectedCharSets) { int start = 0; if ((selectedCharSets & UPPER_ALPHA) > 0) { copyCharsToCharSet(UPPER_ALPHA_CHARS, start); start = start + UPPER_ALPHA_CHARS.length; } if ((selectedCharSets & LOWER_ALPHA) > 0) { copyCharsToCharSet(LOWER_ALPHA_CHARS, start); start = start + LOWER_ALPHA_CHARS.length; } if ((selectedCharSets & NUMERIC) > 0) { copyCharsToCharSet(NUMERIC_CHARS, start); start = start + NUMERIC_CHARS.length; } if ((selectedCharSets & SPECIAL) > 0) { copyCharsToCharSet(SPECIAL_CHARS, start); start = start + SPECIAL_CHARS.length; } if ((selectedCharSets & BRACKET) > 0) { copyCharsToCharSet(BRACKET_CHARS, start); start = start + BRACKET_CHARS.length; } if (customChars != null && customChars.length > 0) { copyCharsToCharSet(customChars, start); start = start + customChars.length; } return start; }; private void copyCharsToCharSet(char[] src, int charSetStart) { System.arraycopy(src, 0, charSet, charSetStart, src.length); } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -