Skip to content

Instantly share code, notes, and snippets.

@GlasFrost
Created November 13, 2015 15:14
Show Gist options
  • Save GlasFrost/6f56caac304b58196fbd to your computer and use it in GitHub Desktop.
Save GlasFrost/6f56caac304b58196fbd to your computer and use it in GitHub Desktop.
Generates random output usable for design purposes. DO NOT USE FOR CRYPTO! YOU HAVE BEEN WARNED!
/*
* Copyright (c) 2015 Luis Hartmann
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package com.glasfrost.labs.randomoutput;
import static java.lang.Math.abs;
import java.util.Date;
import java.util.Random;
/**
*
* @author Luis
*/
public class RandomOutput {
public static final String CHARS_BIN = "01";
public static final String CHARS_OCT = "01234567";
public static final String CHARS_DEC = "0123456789";
public static final String CHARS_HEX = "123456789abcdef";
public static final String CHARS_ALNUM = "0123456789"
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz";
public static final long DEFAULT_AMOUNT = 512;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
if(args.length<1 || args.length>3){
usage();
System.exit(0);
}
long amount = DEFAULT_AMOUNT;
Random rand = new Random();
rand.setSeed(new Date().getTime());
boolean debug = false;
if(args.length>1){
amount = Long.parseLong(args[1]);
}
if(args.length==3 && args[2].equals("debug")){
debug = true;
}
err("main: amount: " + amount, debug);
switch(args[0]){
case "bin":
generate(CHARS_BIN, rand, amount, debug);
break;
case "oct":
generate(CHARS_OCT, rand, amount, debug);
break;
case "dec":
generate(CHARS_DEC, rand, amount, debug);
break;
case "hex":
generate(CHARS_HEX, rand, amount, debug);
break;
case "alnum":
generate(CHARS_ALNUM, rand, amount, debug);
break;
}
err("main: amount: " + amount, debug);
}
public static void generate(String charlist, Random rand, long amount, boolean debug){
char[] chars = charlist.toCharArray();
err("generate: charlist.length(): " + charlist.length(), debug);
for(long count=0; count<amount; count++){
int charnum = charlist.length();
int pick = (abs(rand.nextInt()) % charnum);
err("generate: pick: " + pick, debug);
System.out.print(chars[pick]);
}
System.out.println();
}
public static void err(String msg, boolean debug){
if(debug){
System.err.println(msg);
}
}
public static void usage(){
System.err.println("RandomOutput");
System.err.println("by Luis Hartmann 2015");
System.err.println("");
System.err.println("usage: RandomOutput <mode> [<amount>]");
System.err.println("");
System.err.println(" <mode> can be one of the following:");
System.err.println(" bin");
System.err.println(" Outputs binary chars");
System.err.println(" 0-1");
System.err.println("");
System.err.println(" oct");
System.err.println(" Outputs octal chars");
System.err.println(" 0-7");
System.err.println("");
System.err.println(" dec");
System.err.println(" Outputs decimal chars");
System.err.println(" 0-9");
System.err.println("");
System.err.println(" hex");
System.err.println(" Outputs hexadecimal chars");
System.err.println(" 0-9 a-f");
System.err.println("");
System.err.println(" alnum");
System.err.println(" Outputs alphanumerical chars");
System.err.println(" 0-9 a-z A-Z");
System.err.println("");
System.err.println(" <amount> is optional and defaults to "
+ DEFAULT_AMOUNT + ".");
System.err.println(" It determines how many chars will be "
+ "generated.");
System.err.println("");
System.err.println(" WARNING:");
System.err.println(" DO NOT RELY ON THE RANDOMNESS OF THE OUTPUT!");
System.err.println(" THE CHARS ARE NOT INTENDED FOR CRYPTOGRAPHICAL "
+ "PURPOSES!");
System.err.println(" USE AT OWN RISK!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment