SecureRandom getSeed() method in Java


The seed bytes as required can be obtained using the method getSeed() in the class java.security.SecureRandom. This method requires a single parameter i.e. the number of seed bytes that need to be generated and it returns the seed bytes as required.

A program that demonstrates this is given as follows −

Example

 Live Demo

import java.security.*;
import java.util.*;
public class Demo {
   public static void main(String[] argv) {
      try {
         SecureRandom sRandom = SecureRandom.getInstance("SHA1PRNG");
         byte[] arrB = sRandom.getSeed(5);
         System.out.println("The Seed Bytes in array are: " + Arrays.toString(arrB));
      } catch (NoSuchAlgorithmException e) {
         System.out.println("Error!!! NoSuchAlgorithmException");
      }
   }
}

Output

The Seed Bytes in array are: [-5, -39, 85, -112, 56]

Now let us understand the above program.

The method getSeed() is used to obtain the seed bytes as required. Then the Seed Bytes in the array are displayed. The code snippet that demonstrates this is as follows −

try {
   SecureRandom sRandom = SecureRandom.getInstance("SHA1PRNG");
   byte[] arrB = sRandom.getSeed(5);
   System.out.println("The Seed Bytes in array are: " + Arrays.toString(arrB));
} catch (NoSuchAlgorithmException e) {
   System.out.println("Error!!! NoSuchAlgorithmException");
}

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 30-Jul-2019

77 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements