Java에서 임시 패스워드 만들기.

package xxx.xxx.util;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;

public class PasswordUtil {

  public static final int KEY_LENTH = 2;

  /**
   * 初期パスワードを生成するメソッドです
   * 
   * @throws NoSuchAlgorithmException
   * @returnパスワード
   */
  public static String generateKey() throws NoSuchAlgorithmException {
    String resultStr = null;
    // 候補文字列
    String[] candidates = new String[] { "abcdefghijklmnopqrstuvwxyz",
        "0123456789" };
    int count = KEY_LENTH;
    char[] generated = new char[count];
    SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
    StringBuffer candidateAll = new StringBuffer();
    int watermark = 0;
    for (int i = 0; i < candidates.length; i++) {
      if (candidates[i] != null && !"".equals(candidates[i])) {
        generated[watermark++] = candidates[i].charAt(sr.nextInt(candidates[i].length()));
        candidateAll.append(candidates[i]);
      }
    }
    for (int i = watermark; i < count; i++) {
      generated[i] = candidateAll.toString()
        .charAt(sr.nextInt(candidateAll.length()));
    }
    for (int i = 100000; i >= 0; i--) {
      int x = sr.nextInt(count);
      int y = sr.nextInt(count);
      char tmp = generated[x];
      generated[x] = generated[y];
      generated[y] = tmp;
    }
    resultStr = new String(generated);
    return resultStr;
  }// GEN-LAST:event_jButton1ActionPerformed

}

Leave a Reply

Your email address will not be published. Required fields are marked *