How to generate a secure 80-bit random hexadecimal string in Java

1 Answer

0 votes
import java.math.BigInteger;  
import java.security.SecureRandom;  

public class RandomHexGenerator {  
   public static void main(String[] args) {  
      String hexstr = generateHex(80); // Generates 80-bit hex number // 80 / 4(1 hex) = 20 digits

      System.out.println("Random hexadecimal: " + hexstr);  
   }
   public static String generateHex(int numBits) {  
      SecureRandom random = new SecureRandom();  
      
      // BigInteger(int numBits, Random rnd) - Constructs a randomly generated BigInteger
      BigInteger bigint = new BigInteger(numBits, random);  
      
      return bigint.toString(16);
   }
}
 
 
/*
run:
 
Random hexadecimal: 87f64424c61c0fb8cd57
 
*/

 



answered Sep 19, 2025 by avibootz
...