Merge branch 'master' of gitea.azcona-becker.net:beckermj/framework-python
This commit is contained in:
commit
b42d45a5b8
@ -16,13 +16,36 @@ class UUIDParseResult:
|
|||||||
self.kind = kind
|
self.kind = kind
|
||||||
self.message = message
|
self.message = message
|
||||||
|
|
||||||
|
class URandom:
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
try:
|
||||||
|
self.__urand = open("/dev/urandom", "rb")
|
||||||
|
except:
|
||||||
|
self.__urand = None
|
||||||
|
|
||||||
|
def read(self, length : int) -> bytes:
|
||||||
|
if self.__urand is not None:
|
||||||
|
return self.__urand.read(length)
|
||||||
|
else:
|
||||||
|
# If /dev/urandom isn't available (eg: in non-unix systems), use mt_rand().
|
||||||
|
# __pr_bits = ""
|
||||||
|
# for cnt in range(0, 16):
|
||||||
|
# __pr_bits += chr ( __mt_rand ( 0, 255 ) )
|
||||||
|
pass
|
||||||
|
|
||||||
|
import os
|
||||||
|
return os.urandom(length)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Guid:
|
class Guid:
|
||||||
|
|
||||||
__urand = None
|
__urand = None
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
if Guid.__urand is None:
|
if Guid.__urand is None:
|
||||||
Guid.__urand = open("/dev/urandom", "rb")
|
Guid.__urand = URandom()
|
||||||
|
|
||||||
self.__a = 0
|
self.__a = 0
|
||||||
self.__b = 0
|
self.__b = 0
|
||||||
@ -228,25 +251,20 @@ class Guid:
|
|||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def __init_bits():
|
def __init_bits(self):
|
||||||
__pr_bits = False
|
self.pr_bits = Guid.__urand.read(16)
|
||||||
if Guid.__urand is not None:
|
|
||||||
__pr_bits += Guid.__urand.read(16)
|
self.__a = (int(self.pr_bits[3]) << 24) | (int(self.pr_bits[2]) << 16) | (int(self.pr_bits[1]) << 8) | self.pr_bits[0]
|
||||||
|
self.__b = ((int(self.pr_bits[5]) << 8) | self.pr_bits[4])
|
||||||
if not self.__pr_bits:
|
self.__c = ((int(self.pr_bits[7]) << 8) | self.pr_bits[6])
|
||||||
fp = open ( '/dev/urandom', 'rb' )
|
self.__d = self.pr_bits[8]
|
||||||
if fp is not False:
|
self.__e = self.pr_bits[9]
|
||||||
__pr_bits += fp.read(16)
|
self.__f = self.pr_bits[10]
|
||||||
fp.close()
|
self.__g = self.pr_bits[11]
|
||||||
|
self.__h = self.pr_bits[12]
|
||||||
else:
|
self.__i = self.pr_bits[13]
|
||||||
|
self.__j = self.pr_bits[14]
|
||||||
# If /dev/urandom isn't available (eg: in non-unix systems), use mt_rand().
|
self.__k = self.pr_bits[15]
|
||||||
__pr_bits = ""
|
|
||||||
for cnt in range(0, 16):
|
|
||||||
__pr_bits += chr ( __mt_rand ( 0, 255 ) )
|
|
||||||
|
|
||||||
return __pr_bits
|
|
||||||
|
|
||||||
"""
|
"""
|
||||||
@brief Generates a Universally Unique IDentifier, version 4.
|
@brief Generates a Universally Unique IDentifier, version 4.
|
||||||
@ -261,20 +279,7 @@ class Guid:
|
|||||||
@staticmethod
|
@staticmethod
|
||||||
def generate():
|
def generate():
|
||||||
uuid = Guid()
|
uuid = Guid()
|
||||||
__pr_bits = Guid.__init_bits()
|
uuid.__init_bits()
|
||||||
|
|
||||||
uuid.__a = (int(pr_bits[3]) << 24) | (int(pr_bits[2]) << 16) | (int(pr_bits[1]) << 8) | pr_bits[0]
|
|
||||||
uuid.__b = ((int(pr_bits[5]) << 8) | pr_bits[4])
|
|
||||||
uuid.__c = ((int(pr_bits[7]) << 8) | pr_bits[6])
|
|
||||||
uuid.__d = pr_bits[8]
|
|
||||||
uuid.__e = pr_bits[9]
|
|
||||||
uuid.__f = pr_bits[10]
|
|
||||||
uuid.__g = pr_bits[11]
|
|
||||||
uuid.__h = pr_bits[12]
|
|
||||||
uuid.__i = pr_bits[13]
|
|
||||||
uuid.__j = pr_bits[14]
|
|
||||||
uuid.__k = pr_bits[15]
|
|
||||||
|
|
||||||
return uuid
|
return uuid
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user