1 20 21 22 23 24 package org.snmp4j.security; 25 26 import java.util.Random ; 27 import java.security.SecureRandom ; 28 import java.security.NoSuchAlgorithmException ; 29 import org.snmp4j.log.*; 30 31 41 class Salt { 42 private long salt; 43 44 private static Salt instance = null; 45 private static final LogAdapter logger = LogFactory.getLogger(Salt.class); 46 47 50 protected Salt() { 51 byte[] rnd = new byte[8]; 52 53 try { 54 SecureRandom sr = SecureRandom.getInstance("SHA1PRNG"); 55 sr.nextBytes(rnd); 56 } 57 catch (NoSuchAlgorithmException nsae) { 58 logger.warn("Could not use SecureRandom. Using Random instead."); 59 Random r = new Random (); 60 r.nextBytes(rnd); 61 } 62 63 salt = rnd[0]; 64 65 for (int i = 0; i < 7; i++) { 66 salt = (salt * 256) + ((int)rnd[i]) + 128; 67 } 68 if (logger.isDebugEnabled() == true) { 69 logger.debug("Initialized Salt to " + Long.toHexString(salt) + "."); 70 } 71 } 72 73 78 public static Salt getInstance() { 79 if (instance == null) { 80 instance = new Salt(); 81 } 82 return instance; 83 } 84 85 91 public synchronized long getNext() { 92 return salt++; 93 } 94 } 95 | Popular Tags |