ã©ã³ãã æååçæ
å
æ¥CodeIQã§ã©ã³ãã ãã¹ã¯ã¼ããçæããããã°ã©ã ã®åé¡(挑戦者求む!【ウチに来ない?】[Java]ランダムパスワードの作成 by CodeIQ運営事務局 アプリケーションエンジニアを募集する企業│CodeIQ)
ã§è§£çããéã«ä½¿ã£ãã©ã³ãã æååçæã®ã¡ã¢ã§ãã
ããããã©ã³ãã æååãä½ãã«ã¯ããã¤ãã®æ¹æ³ãããã¨æãã¾ããã
åãæãã¤ããã®ã¯ä»¥ä¸ã®5ã¤ã§ããã
UUID
å®ã¯UUIDãçæããéã«ã以ä¸ã®ã¯ã©ã¹ã®ã©ã¡ãã使ã£ã¦ãçæã§ããã¿ããã
ã»org.w3c.util.UUID
ã»java.util.UUID
ãã ãorg.w3c.util.UUIDã®æ¹ã¯æ¯å10msã®sleepãå¼ãã§ããä»æ§ã¨ãªã£ã¦ãã®ã§é度ãæ°ã«ããã®ã§ããã°ã使ç¨ããªãæ¹ãããããã
ãã¨UUIDã¯ãã£ã¡ã§æååã¯æå®ã§ããªãã®ã§ãè±æ°åã ãã¨ããè¨å·ã®ã¿ã§ã®æååãã©ã³ãã çæãããå ´åã¯ä½¿ããªãã®ã§æ³¨æã
â»ã¡ãªã¿ã«å
é¨ã§ã¯SecureRandomã使ã£ã¦ããã
ãµã³ãã«ã³ã¼ã
ããpublic static void main(String args[]) { UUID id = UUID.randomUUID(); System.out.println(id); }
çµæï¼0c15831c-68a4-4df0-89eb-bf992070ed0
java.util.Random
åããåç¾æ§ããããã¨ã§æåãªã®ã§ããã¾ã使ãã¨ãããã¨ã¯å°ãªãã¨æãã¾ãããåèã¾ã§ã«ã
主ã«æå»ãseedã«ãã¦Randomãçæããã®ãå¤ãã®ããªã¨æãã¾ãããå®éã«Randomã®ä¸ãè¦ãã¨ãã³ã³ã¹ãã©ã¯ã¿ã§ä»¥ä¸ã®ãã¨ããã¦ããã®ã§ã
public Random() { this(++seedUniquifier + System.nanoTime()); } |
ãããµã³ãã«ã§è¦ãã
Random random = new Random(System.currentTimeMillis()); |
ã¯ããããæå®ããå¿ è¦ã¯ãªãã¨æãã
ãµã³ãã«ã³ã¼ã
public static void main(String args[]) { Random random = new Random(); System.out.println(random.nextDouble()); }
çµæï¼0.7308781907032909
java.lang.Math#Random
Oracleã®ããã¥ã¡ã³ãã«ãããããã«å®ã¯Randomãããã¡ãã®ãç°¡åã§ããã¨è¨ã£ã¦ãã¾ãã
http://docs.oracle.com/javase/jp/6/api/java/util/Random.html
ãã ããã¡ããå®éã¯å
é¨ã§java.util.Randomã使ç¨ãã¦ããã®ã§ãåãã¨ããç¹ã§ã¯æ¥µå使ç¨ã¯æ§ããæ¹ããããããããªãã
ãµã³ãã«ã³ã¼ã
public static void main(String args[]) { System.out.println(Math.random()); }
çµæï¼0.9219827452200374
org.apache.commons.lang3.RandomStringUtils
便å©ãªã©ã³ãã æååçæã¯ã©ã¹ã¨ãã£ãæãã
è±åã ããæ°åã ããæåæ°æå®ãã¨ããç°¡åã«è¨è¿°ã§ããç¹ã§ã¯ããªã便å©ã ãã©ãæ®å¿µãªãã¨ã«ä»ã¨åæ§ã§å
é¨ã§java.util.Randomã使ã£ã¦ãã®ã§ãçæããæååãã¯ãªãã£ã«ã«ãªå ´åã¯ãã£ã±ã極å使ç¨ã¯ããªãæ¹ãããã
ãµã³ãã«ã³ã¼ã
public static void main(String args[]) { // åè§æ°åã使ç¨ã§ã©ã³ãã æååãçæ String random1 = RandomStringUtils.randomNumeric(10); // åè§ã¢ã«ãã¡ããã + åè§æ°åã§ã©ã³ãã æååãçæ String random2 = RandomStringUtils.randomAlphanumeric(10); // æå®ããæååã§ã©ã³ãã æååãçæ String random3 = RandomStringUtils.random(10, "ABC123!%&'"); System.out.println(random1); System.out.println(random2); System.out.println(random3); }
çµæï¼
random1ï¼0504193294
random2ï¼vd1FjlZDGg
random3ï¼3'&%A'!C2A
java.security.SecureRandom
Randomã¨éã£ã¦ãæå·ã¢ã«ã´ãªãºã ãè¨å®ãããã¨ãã§ããã¸ã§ãã¬ã¼ã¿ã¼ã§ãOSã®ã«ãã£ã¦ä½¿ãã¢ã«ã´ãªãºã ãéããããwindowsã ã¨"SHA1PRNG"ãLinuxã ã¨"NativePRNG"ãããã©ã«ãã
ãã¡ããç´æ¥æå®ãããã¨ãã§ããã
ãã ã"NativePRNG"ã®æ¹ãå¦çãé
ãã¨ããæ
å ±ããã£ããããã®ã§ãä¸è¬çãª"SHA1PRNG"æå®ããæ¹ãããã®ããªã£ã¦æãã¾ãã
ã¾ããjava.security.SecureRandom.nextBytes(byte[])
ãå¼ã¶ã¨ã»ãã¥ã¢ãªæ¹æ³ã§seedãèªåçã«è¨å®ã§ããã®ã§ãæçµçã«ã¯ä»¥ä¸ã®æ§ãªå®è£
ã«ãªãã
â»SHA1PRNGã/dev/randomããèªã¿è¾¼ããã¨ãã¦ã/dev/urandomãé¸æããã¦ãã¾ããã°ããããããã®ã§åèã¾ã§ã«ãhttp://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6202721
ãµã³ãã«ã³ã¼ã
ããpublic static void main(String args[]) { ãã SecureRandom secRandom = null; ããbyte bytes[] = new byte[16]; try { secRandom = SecureRandom.getInstance("SHA1PRNG"); secRandom.nextBytes(bytes); } catch (NoSuchAlgorithmException e) { ãã System.out.println("ãããªã¢ã«ã´ãªãºã ã¯ãªãã"); } System.out.println(secRandom.nextDouble()); }
çµæï¼0.9546535293824232
çµè«
æçµçã«ä½ãç®çã¨ãã¦ã©ã³ãã æååãçæãããã«ãã£ã¦ãé¸æãããã®ãå¤ãã£ã¦ããã®ããªã¨æãã
ããã©ã¼ãã³ã¹ããã»ãã¥ãªãã£éè¦ãªãééããªãSecureRandomã¨ãUUID使ããã¨ã«ãªãã ãããããã¾ããªããããããªããã©ãåç´ã«ã©ã³ãã æååãçæãããã®ã§ããã°ãä¸è¨ã®å¥½ããªä¹±æ°çºçå¨ã使ç¨ãã¦åé¡ãªãã¯ãã
ã
ãè±èªã®è¨äºã§ããã以ä¸ã®ãµã¤ãã¯çµæ§æ·±ãã¨ããã«çªã£è¾¼ãã§ããã®ã§ãããã£ããåèã«ãã¦ã¿ã¦ãã ããã
ãã»http://www.cigital.com/justice-league-blog/2009/08/14/proper-use-of-javas-securerandom/
ãã»http://moi.vonos.net/java/securerandom/
ãã»http://resources.infosecinstitute.com/random-number-generation-java/