PATs

We can create PATs using a big integer, and PATs can be sued for remove access as opposed to an OTP. An OTP would have to be different, while a PAT can be issues as soon as the user is created and there by be kept consistent. Which one we’ll use is up to the decision of the team overall at the start of next week.

import java.security.SecureRandom;
import java.math.BigInteger;
import java.util.ArrayList;

public class PATExperiment {

    public static String generateUniqueToken(int length) {
        SecureRandom secureRandom = new SecureRandom();
        byte[] randomBytes = new byte[length / 2];
        secureRandom.nextBytes(randomBytes);
        BigInteger bigInteger = new BigInteger(1, randomBytes);
        String token = bigInteger.toString(16);

        // Ensure the token has the specified length
        while (token.length() < length) {
            token = "0" + token;
        }

        return token;
    }

    public static void main(String[] args) {
        // Example: Generate a unique token of length 32
        ArrayList<String> PATs = new ArrayList<>();

        for(int i = 0; i < 10; i++){
            String pat = generateUniqueToken(32);
            System.out.println("Generated PAT: " + pat);
            PATs.add(pat); // we can then store the PAT. Thi
        }
        
    }
}
PATExperiment.main(null)
Generated PAT: 42481c961d5d228c93a99e862b1f4201
Generated PAT: 21f038fe7c42cfd2a404692cd71991c2
Generated PAT: b2d7072c2d0bad660752b92f7bdb42fa
Generated PAT: 19e00960f52207b2147b49a3bc917c94
Generated PAT: aa9d08ca005dac7ce7326e50617facb8
Generated PAT: 492dfb45d03a69160de71a5ce6864763
Generated PAT: e99d5bc3d0204c7af44f60f860529f93
Generated PAT: fe17d49a691bc3a5aa697e4e104802c1
Generated PAT: 245beb3f46b6b9be731cf1f5310264b2
Generated PAT: 43bcd6a145d820f1ca2d53a2265d09a9

One-Time Passwords

One time passwords can be shorter, easily sent, and can probably fulfill more of the requirements. However it can by system intensive to send out a ton of OTP is people are waiting.

import java.security.SecureRandom;
import java.math.BigInteger;
import java.util.HashSet;
import java.util.Set;

public class OTPGenerator {

    public static String generateOTP(int length) {
        SecureRandom secureRandom = new SecureRandom();
        byte[] randomBytes = new byte[length / 2];
        secureRandom.nextBytes(randomBytes);
        BigInteger bigInteger = new BigInteger(1, randomBytes);
        String otp = bigInteger.toString(10);

        // Ensure the OTP has the specified length
        while (otp.length() < length) {
            otp = "0" + otp;
        }

        return otp;
    }

    public static Set<String> generateUniqueOTPs(int count, int length) {
        Set<String> uniqueOTPs = new HashSet<>();

        while (uniqueOTPs.size() < count) {
            String otp = generateOTP(length);
            uniqueOTPs.add(otp);
        }

        return uniqueOTPs;
    }

    public static void main(String[] args) {
        Set<String> uniqueOTPs = generateUniqueOTPs(10, 6); // TODO: need to fix why we have not a 6-int APT

        System.out.println("Generated Unique OTPs:");
        for (String otp : uniqueOTPs) {
            System.out.println(otp); 
        }
    }
}

OTPGenerator.main(null);
Generated Unique OTPs:
8923430
7755135
12656019
5184962
14805371
9338128
3964740
16455686
3866514
13154224