SHARE
TWEET

QuantumGuid.py

waterjuice Aug 29th, 2019 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/env python3
  2. #-----------------------------------------------------------------------------------------------------------------------
  3. #   QuantumGuid
  4. #
  5. #   Creates 1 or more Version 4 GUIDs using the Quantum random number generator service by Australia National
  6. #   University (ANU)
  7. #
  8. #   Compatibilty: Python 3 (Verified on 3.7)
  9. #
  10. #   This is free and unencumbered software released into the public domain - August 2019 waterjuice.org
  11. #-----------------------------------------------------------------------------------------------------------------------
  12.  
  13. #-----------------------------------------------------------------------------------------------------------------------
  14. #   IMPORTS
  15. #-----------------------------------------------------------------------------------------------------------------------
  16.  
  17. import urllib.request
  18. import ssl
  19. import json
  20. import uuid
  21. import sys
  22.  
  23. #-----------------------------------------------------------------------------------------------------------------------
  24. #   FUNCTIONS
  25. #-----------------------------------------------------------------------------------------------------------------------
  26.  
  27. #-----------------------------------------------------------------------------------------------------------------------
  28. #   main
  29. #-----------------------------------------------------------------------------------------------------------------------
  30. def main( ArgV ):
  31.     if( len(ArgV) != 2 ):
  32.         print( "Syntax:\n  QuantumGuid.py <NumGuids>")
  33.         return 1
  34.     numGuids = int( ArgV[1] )
  35.  
  36.     if numGuids < 1 or numGuids > 1000:
  37.         print( "Number of GUIDs must be between 1 and 1000")
  38.         return 1
  39.  
  40.     # Connect to the online service for QRNG at ANU    
  41.     url = 'https://qrng.anu.edu.au/API/jsonI.php?length=%u&type=hex16&size=16' % numGuids
  42.     response = urllib.request.urlopen(url)
  43.     data = response.read()
  44.     text = data.decode('utf-8')
  45.     # Convert json
  46.     jdata = json.loads( text )
  47.  
  48.     if not jdata['success']:
  49.         print( "Failed to get data" )
  50.         return 2
  51.     if not jdata['type'] == 'string':
  52.         print( "Wrong format of data returned from service (expected string)" )
  53.         return 2
  54.     if not jdata['size'] == 16:
  55.         print( "Wrong amount of data returned" )
  56.         return 2
  57.  
  58.     # Process all the 16 byte random numbers. They need to be converted into type 4 GUIDs by setting the appropriate
  59.     # 6 fixed bits.
  60.     for hexStr in jdata['data']:
  61.         guidBytes = bytearray.fromhex( hexStr )
  62.         guidBytes[6] &= 0x0f   #  Mask out version bits
  63.         guidBytes[6] |= 0x40   #  Set version bits to 4 (Random)
  64.         guidBytes[8] &= 0x3f   #  Mask out variant bits
  65.         guidBytes[8] |= 0x80   #  Set variant bits to RFC4122 variant
  66.  
  67.         # Convert to a GUID
  68.         guid = uuid.UUID( bytes=bytes(guidBytes) )
  69.  
  70.         # Print it (Adding the curly braces)
  71.         print( "{%s}" % str(guid) )
  72.  
  73. #-----------------------------------------------------------------------------------------------------------------------
  74. #   Main Entry Point
  75. #-----------------------------------------------------------------------------------------------------------------------
  76. if __name__ == "__main__":
  77.     exit( main( sys.argv ) )
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top