LoginSignup
37
32

More than 5 years have passed since last update.

Javaで仮パスワードとかを作るときはcommons-langのRandomStringUtilsが便利

Last updated at Posted at 2016-04-20

はじめに

仕事で仮パスワードを発行する仕組みを作成しているので、
なんか適当なライブラリが無いか調べたのでまとめる。

使い方

commons-langをmavenなりgradleなりで通す。

pom.xml
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-lang3</artifactId>
    <version>3.4</version>
</dependency>

サンプル

RandomStringTest.java
import org.apache.commons.lang3.RandomStringUtils;

public class RandomStringTest {

    public static void main(String[] args) {

        // アルファベット16桁
        System.out.println(RandomStringUtils.randomAlphabetic(16));
        // アスキー16桁
        System.out.println(RandomStringUtils.randomAscii(16));
        // 数字16桁
        System.out.println(RandomStringUtils.randomNumeric(16));
        // アルファベットと数字16桁
        System.out.println(RandomStringUtils.randomAlphanumeric(16));
        // 自分で設定した文字列からランダム16桁
        System.out.println(RandomStringUtils.random(16, "abcdefg"));

    }
}
実行結果
HXJcdIiVFbIUwvIe
}RPuODdHc%)BR[k'
8391904488624867
MVhTqGAzdf00kG2C
acefeaeeeadgcagg

すごく簡単。

37
32
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
37
32