Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- #-----------------------------------------------------------------------------------------------------------------------
- # QuantumGuid
- #
- # Creates 1 or more Version 4 GUIDs using the Quantum random number generator service by Australia National
- # University (ANU)
- #
- # Compatibilty: Python 3 (Verified on 3.7)
- #
- # This is free and unencumbered software released into the public domain - August 2019 waterjuice.org
- #-----------------------------------------------------------------------------------------------------------------------
- #-----------------------------------------------------------------------------------------------------------------------
- # IMPORTS
- #-----------------------------------------------------------------------------------------------------------------------
- import urllib.request
- import ssl
- import json
- import uuid
- import sys
- #-----------------------------------------------------------------------------------------------------------------------
- # FUNCTIONS
- #-----------------------------------------------------------------------------------------------------------------------
- #-----------------------------------------------------------------------------------------------------------------------
- # main
- #-----------------------------------------------------------------------------------------------------------------------
- def main( ArgV ):
- if( len(ArgV) != 2 ):
- print( "Syntax:\n QuantumGuid.py <NumGuids>")
- return 1
- numGuids = int( ArgV[1] )
- if numGuids < 1 or numGuids > 1000:
- print( "Number of GUIDs must be between 1 and 1000")
- return 1
- # Connect to the online service for QRNG at ANU
- url = 'https://qrng.anu.edu.au/API/jsonI.php?length=%u&type=hex16&size=16' % numGuids
- response = urllib.request.urlopen(url)
- data = response.read()
- text = data.decode('utf-8')
- # Convert json
- jdata = json.loads( text )
- if not jdata['success']:
- print( "Failed to get data" )
- return 2
- if not jdata['type'] == 'string':
- print( "Wrong format of data returned from service (expected string)" )
- return 2
- if not jdata['size'] == 16:
- print( "Wrong amount of data returned" )
- return 2
- # Process all the 16 byte random numbers. They need to be converted into type 4 GUIDs by setting the appropriate
- # 6 fixed bits.
- for hexStr in jdata['data']:
- guidBytes = bytearray.fromhex( hexStr )
- guidBytes[6] &= 0x0f # Mask out version bits
- guidBytes[6] |= 0x40 # Set version bits to 4 (Random)
- guidBytes[8] &= 0x3f # Mask out variant bits
- guidBytes[8] |= 0x80 # Set variant bits to RFC4122 variant
- # Convert to a GUID
- guid = uuid.UUID( bytes=bytes(guidBytes) )
- # Print it (Adding the curly braces)
- print( "{%s}" % str(guid) )
- #-----------------------------------------------------------------------------------------------------------------------
- # Main Entry Point
- #-----------------------------------------------------------------------------------------------------------------------
- if __name__ == "__main__":
- exit( main( sys.argv ) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement