Commit 9756f012 by Pedro Cavaleiro

Implemented random pin generator, removed unecessary comments

parent 028da0bb
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package fallintooblivion; package fallintooblivion;
import javax.crypto.Cipher; import javax.crypto.Cipher;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.security.SecureRandom;
import java.util.Arrays; import java.util.Arrays;
...@@ -286,14 +282,20 @@ public class Helpers { ...@@ -286,14 +282,20 @@ public class Helpers {
String outFile = "Fall_Into_Oblivion/Trashed/" + fileName + "/" + fileName; String outFile = "Fall_Into_Oblivion/Trashed/" + fileName + "/" + fileName;
// TEMPORARY debugging purposes only
// Create the hash of the pin 0000
// So far we were only able to use 16 Byte key // So far we were only able to use 16 Byte key
// 24 Byte or 32 Byte will say Invalid Key Size, even though it's a valid key size // 24 Byte or 32 Byte will say Invalid Key Size, even though it's a valid key size
// TODO: Randomize 3 or 4 digit pin without printing it to the user RandomString pinGenerator = new RandomString(4, new SecureRandom(), RandomString.Symbols.digits);
String pinHASH = SHA256.calculateStringMAC("0000"); String genPin = pinGenerator.nextString();
String pinHASH = SHA256.calculateStringMAC(genPin);
pinHASH = pinHASH.subSequence(0, 16).toString(); pinHASH = pinHASH.subSequence(0, 16).toString();
// System.out.println(genPin); // Uncomment this line to view the generated pin
// Uncomment the two lines below to generate the key to the pin 0000
// String pinHASH = SHA256.calculateStringMAC("0000");
// pinHASH = pinHASH.subSequence(0, 16).toString();
// Encrypt the file using the defined cypher type, it defaults to AES-CBC // Encrypt the file using the defined cypher type, it defaults to AES-CBC
switch (cypher) { switch (cypher) {
case "aes_cbc": case "aes_cbc":
......
package fallintooblivion;
import java.security.SecureRandom;
import java.util.Locale;
import java.util.Objects;
import java.util.Random;
public class RandomString {
/**
* Generate a random string.
*/
public String nextString() {
for (int idx = 0; idx < buf.length; ++idx)
buf[idx] = symbols[random.nextInt(symbols.length)];
return new String(buf);
}
public enum Symbols {
uppercase,
lowercase,
digits,
alphanumeric,
upperlowercase
}
public static final String upper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static final String lower = upper.toLowerCase(Locale.ROOT);
public static final String digits = "0123456789";
public static final String alphanum = upper + lower + digits;
private final Random random;
private final char[] symbols;
private final char[] buf;
public RandomString(int length, Random random, String symbols) {
if (length < 1) throw new IllegalArgumentException();
if (symbols.length() < 2) throw new IllegalArgumentException();
this.random = Objects.requireNonNull(random);
this.symbols = symbols.toCharArray();
this.buf = new char[length];
}
public RandomString(int length, Random random, Symbols symbols) {
switch (symbols) {
case digits:
this.random = Objects.requireNonNull(random);
this.symbols = digits.toCharArray();
this.buf = new char[length];
break;
case lowercase:
this.random = Objects.requireNonNull(random);
this.symbols = lower.toCharArray();
this.buf = new char[length];
break;
case uppercase:
this.random = Objects.requireNonNull(random);
this.symbols = upper.toCharArray();
this.buf = new char[length];
break;
case alphanumeric:
this.random = Objects.requireNonNull(random);
this.symbols = alphanum.toCharArray();
this.buf = new char[length];
break;
case upperlowercase:
String alphabet = upper + lower;
this.random = Objects.requireNonNull(random);
this.symbols = alphabet.toCharArray();
this.buf = new char[length];
break;
default:
this.random = Objects.requireNonNull(random);
this.symbols = alphanum.toCharArray();
this.buf = new char[length];
break;
}
}
/**
* Create an alphanumeric string generator.
*/
public RandomString(int length, Random random) {
this(length, random, alphanum);
}
/**
* Create an alphanumeric strings from a secure generator.
*/
public RandomString(int length) {
this(length, new SecureRandom());
}
/**
* Create session identifiers.
*/
public RandomString() {
this(21);
}
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment