RandomGenerator.st
changeset 4250 c21944d9e5d7
parent 4041 ed60d9c91792
child 4482 6a22682659e2
equal deleted inserted replaced
4249:d70b0b1791a7 4250:c21944d9e5d7
    13 
    13 
    14 "{ NameSpace: Smalltalk }"
    14 "{ NameSpace: Smalltalk }"
    15 
    15 
    16 Random subclass:#RandomGenerator
    16 Random subclass:#RandomGenerator
    17 	instanceVariableNames:''
    17 	instanceVariableNames:''
    18 	classVariableNames:'HasOSRandom RandFile SharedRandomGenerator RandomDevicePathes'
    18 	classVariableNames:'HasOSRandom RandFile RandomDevicePathes SharedRandomGenerator'
    19 	poolDictionaries:''
    19 	poolDictionaries:''
    20 	category:'Magnitude-Numbers'
    20 	category:'Magnitude-Numbers'
    21 !
    21 !
    22 
    22 
    23 !RandomGenerator class methodsFor:'documentation'!
    23 !RandomGenerator class methodsFor:'documentation'!
    95 
    95 
    96 !RandomGenerator class methodsFor:'instance creation'!
    96 !RandomGenerator class methodsFor:'instance creation'!
    97 
    97 
    98 new
    98 new
    99     "return a new random number generator.
    99     "return a new random number generator.
   100      Try to get system random numbers from device (e.g. in LINUX).
   100      Try to get system random numbers from OS (e.g. in LINUX)
       
   101      or device urandom (e.g. in Linux, OSX).
   101      If no system random numbers are available, fall back to
   102      If no system random numbers are available, fall back to
   102      a cryptographic secure PRNG (part of the extra libcrypt package).
   103      a cryptographic secure PRNG (like RC4Random, part of the extra libcrypt package).
   103      As last resort fallback to the cryptographic insecure linear builtin PRNG"
   104      As last resort fallback to the cryptographic insecure linear builtin PRNG"
   104 
   105 
   105     |result|
   106     |result|
   106 
   107 
   107     SharedRandomGenerator notNil ifTrue:[
   108     SharedRandomGenerator notNil ifTrue:[
   108         "each time, we do an new, add some entropy to the SharedGenerator"
   109         "each time, we do a new, add some entropy to the SharedGenerator"
   109         SharedRandomGenerator addEntropy:OperatingSystem getMicrosecondTime.
   110         SharedRandomGenerator addEntropy:OperatingSystem getMicrosecondTime.
   110         ^ SharedRandomGenerator.
   111         ^ SharedRandomGenerator.
   111     ].
   112     ].
   112 
   113 
   113     HasOSRandom isNil ifTrue:[
   114     HasOSRandom isNil ifTrue:[
   185     ].
   186     ].
   186     ^ nil.
   187     ^ nil.
   187 !
   188 !
   188 
   189 
   189 randomDevicePathes
   190 randomDevicePathes
   190     "paths to look for OS sources of random numbers"
   191     "paths to look for OS sources of random numbers.
       
   192      Can be configured at early startup to use something special
       
   193      (i.e. a special hardware random device)
       
   194      If never set, the defaults /dev/random, /dev/urandom are used"
   191 
   195 
   192     RandomDevicePathes notNil ifTrue:[^ RandomDevicePathes].
   196     RandomDevicePathes notNil ifTrue:[^ RandomDevicePathes].
   193     OperatingSystem isUNIXlike ifTrue:[
   197     OperatingSystem isUNIXlike ifTrue:[
   194         ^ #( '/dev/urandom' '/dev/random' )
   198         ^ #( '/dev/urandom' '/dev/random' )
   195     ].
   199     ].
   196     ^ nil.
   200     ^ nil.
   197 !
   201 !
   198 
   202 
   199 randomDevicePathes:aCollectionOfpathesOrNil
   203 randomDevicePathes:aCollectionOfPathesOrNil
   200     "configurable paths to look for OS sources of random numbers.
   204     "configurable paths to look for OS sources of random numbers.
   201      (can be set during early startup in an rc-file)"
   205      (can be set during early startup in an rc-file).
   202 
   206      If never set, the defaults /dev/random, /dev/urandom are used"
   203     RandomDevicePathes := aCollectionOfpathesOrNil.
   207 
       
   208     RandomDevicePathes := aCollectionOfPathesOrNil.
   204 ! !
   209 ! !
   205 
   210 
   206 !RandomGenerator methodsFor:'basic reading'!
   211 !RandomGenerator methodsFor:'basic reading'!
   207 
   212 
   208 nextByte
   213 nextByte