- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
SecureRandom setSeed() method in Java
The random object can be reseeded using the setSeed() method in the class java.security.SecureRandom. This method requires a single parameter i.e. the required seed byte array and it returns the reseeded random object.
A program that demonstrates this is given as follows −
Example
import java.security.*; import java.util.*; public class Demo { public static void main(String[] argv) { try { SecureRandom sRandom = SecureRandom.getInstance("SHA1PRNG"); String str = "Apple"; byte[] arrB = str.getBytes(); sRandom.setSeed(arrB); byte[] arrSeed = sRandom.getSeed(5); System.out.println("The reseeded Byte array is:"); for (int i = 0; i < arrSeed.length; i++) { System.out.print(arrSeed[i] + " "); } } catch (NoSuchAlgorithmException e) { System.out.println("Error!!! NoSuchAlgorithmException"); } catch (ProviderException e) { System.out.println("Error!!! ProviderException"); } } }
Output
The reseeded Byte array is: -88 71 -32 -124 55
Now let us understand the above program.
The setSeed() method is used to reseed the random object. Then the reseeded Byte array is displayed. The code snippet that demonstrates this is as follows −
try { SecureRandom sRandom = SecureRandom.getInstance("SHA1PRNG"); String str = "Apple"; byte[] arrB = str.getBytes(); sRandom.setSeed(arrB); byte[] arrSeed = sRandom.getSeed(5); System.out.println("The reseeded Byte array is:"); for (int i = 0; i < arrSeed.length; i++) { System.out.print(arrSeed[i] + " "); } }
Advertisements