added status, base64, randbytes redesign to minimize hw calls
This commit is contained in:
parent
cb57e489ad
commit
3fe4b5be81
3 changed files with 77 additions and 22 deletions
Binary file not shown.
|
|
@ -25,6 +25,7 @@ import threading
|
||||||
import json
|
import json
|
||||||
import socket
|
import socket
|
||||||
import requests
|
import requests
|
||||||
|
import base64
|
||||||
|
|
||||||
from flask import Flask, request, Response
|
from flask import Flask, request, Response
|
||||||
from flask_sockets import Sockets
|
from flask_sockets import Sockets
|
||||||
|
|
@ -88,6 +89,17 @@ def serve(servername, port, id):
|
||||||
except (TypeError, ValueError) as e:
|
except (TypeError, ValueError) as e:
|
||||||
return Response(str(e), status=400, content_type='text/plain')
|
return Response(str(e), status=400, content_type='text/plain')
|
||||||
|
|
||||||
|
@app.route('/api/randbase64')
|
||||||
|
def randbase64():
|
||||||
|
try:
|
||||||
|
length = int(request.args.get('length'))
|
||||||
|
if length < 1:
|
||||||
|
return Response('length must be greater than 0', status=400, content_type='text/plain')
|
||||||
|
return Response(base64.b64encode(qng_wrapper.randbytes(length)).decode('utf-8'), content_type='text/plain')
|
||||||
|
except (TypeError, ValueError) as e:
|
||||||
|
return Response(str(e), status=400, content_type='text/plain')
|
||||||
|
|
||||||
|
|
||||||
@app.route('/api/randbytes')
|
@app.route('/api/randbytes')
|
||||||
def randbytes():
|
def randbytes():
|
||||||
try:
|
try:
|
||||||
|
|
@ -98,67 +110,103 @@ def serve(servername, port, id):
|
||||||
except (TypeError, ValueError) as e:
|
except (TypeError, ValueError) as e:
|
||||||
return Response(str(e), status=400, content_type='text/plain')
|
return Response(str(e), status=400, content_type='text/plain')
|
||||||
|
|
||||||
|
@app.route('/api/clear')
|
||||||
|
def clear():
|
||||||
|
qng_wrapper.clear()
|
||||||
|
return Response(status=204)
|
||||||
|
|
||||||
|
@app.route('/api/reset')
|
||||||
|
def reset():
|
||||||
|
qng_wrapper.reset()
|
||||||
|
return Response(status=204)
|
||||||
|
|
||||||
|
@app.route('/api/status')
|
||||||
|
def statusstring():
|
||||||
|
status = str(qng_wrapper.statusString())
|
||||||
|
return Response(json.dumps({"server" : servername, "device": id, "status": status}), status=400, content_type='text/plain')
|
||||||
|
|
||||||
# JSON API ----------------------------------------------
|
# JSON API ----------------------------------------------
|
||||||
|
|
||||||
@app.route('/api/json/randint32')
|
@app.route('/api/json/randint32')
|
||||||
def randjsonint32():
|
def randjsonint32():
|
||||||
try:
|
try:
|
||||||
|
status = str(qng_wrapper.statusString())
|
||||||
length = int(request.args.get('length'))
|
length = int(request.args.get('length'))
|
||||||
if length < 1:
|
if length < 1:
|
||||||
return Response(json.dumps({"error": 'length must be greater than 0', "success":"false"}), status=400, content_type='text/plain')
|
return Response(json.dumps({"error": 'length must be greater than 0', "success":"false"}), status=400, content_type='text/plain')
|
||||||
int32array = []
|
int32array = []
|
||||||
for x in range(0, length):
|
for x in range(0, length):
|
||||||
int32array.append(qng_wrapper.randint32())
|
int32array.append(qng_wrapper.randint32())
|
||||||
return Response(json.dumps({"server" : servername, "device": id, "type": "string", "format": "int32", "length":length, "data": int32array, "success": "true"}), content_type='text/plain')
|
return Response(json.dumps({"server" : servername, "device": id, "status": status, "type": "string", "format": "int32", "length":length, "data": int32array, "success": "true"}), content_type='text/plain')
|
||||||
except (TypeError, ValueError) as e:
|
except (TypeError, ValueError) as e:
|
||||||
return Response(json.dumps({"error": str(e), "success":"false"}), status=400, content_type='text/plain')
|
return Response(json.dumps({"error": str(e), "status": status, "success":"false"}), status=400, content_type='text/plain')
|
||||||
|
|
||||||
@app.route('/api/json/randuniform')
|
@app.route('/api/json/randuniform')
|
||||||
def randjsonuniform():
|
def randjsonuniform():
|
||||||
try:
|
try:
|
||||||
|
status = str(qng_wrapper.statusString())
|
||||||
length = int(request.args.get('length'))
|
length = int(request.args.get('length'))
|
||||||
if length < 1:
|
if length < 1:
|
||||||
return Response(json.dumps({"error": 'length must be greater than 0', "success":"false"}), status=400, content_type='text/plain')
|
return Response(json.dumps({"error": 'length must be greater than 0', "success":"false"}), status=400, content_type='text/plain')
|
||||||
uniformarray = []
|
uniformarray = []
|
||||||
for x in range(0, length):
|
for x in range(0, length):
|
||||||
uniformarray.append(qng_wrapper.randuniform())
|
uniformarray.append(qng_wrapper.randuniform())
|
||||||
return Response(json.dumps({"server" : servername, "device": id, "type": "string", "format": "uniform", "length":length, "data": uniformarray, "success": "true"}), content_type='text/plain')
|
return Response(json.dumps({"server" : servername, "device": id, "status": status, "type": "string", "format": "uniform", "length":length, "data": uniformarray, "success": "true"}), content_type='text/plain')
|
||||||
except (TypeError, ValueError) as e:
|
except (TypeError, ValueError) as e:
|
||||||
return Response(json.dumps({"error": str(e), "success":"false"}), status=400, content_type='text/plain')
|
return Response(json.dumps({"error": str(e), "device": id, "status": status, "success":"false"}), status=400, content_type='text/plain')
|
||||||
|
|
||||||
@app.route('/api/json/randnormal')
|
@app.route('/api/json/randnormal')
|
||||||
def randjsonnormal():
|
def randjsonnormal():
|
||||||
try:
|
try:
|
||||||
|
status = str(qng_wrapper.statusString())
|
||||||
length = int(request.args.get('length'))
|
length = int(request.args.get('length'))
|
||||||
if length < 1:
|
if length < 1:
|
||||||
return Response(json.dumps({"error": 'length must be greater than 0', "success":"false"}), status=400, content_type='text/plain')
|
return Response(json.dumps({"error": 'length must be greater than 0', "success":"false"}), status=400, content_type='text/plain')
|
||||||
normarray = []
|
normarray = []
|
||||||
for x in range(0, length):
|
for x in range(0, length):
|
||||||
normarray.append(qng_wrapper.randnormal())
|
normarray.append(qng_wrapper.randnormal())
|
||||||
return Response(json.dumps({"server" : servername, "device": id, "type": "string", "format": "normal", "length":length, "data": normarray, "success": "true"}), content_type='text/plain')
|
return Response(json.dumps({"server" : servername, "device": id, "status": status, "type": "string", "format": "normal", "length":length, "data": normarray, "success": "true"}), content_type='text/plain')
|
||||||
except (TypeError, ValueError) as e:
|
except (TypeError, ValueError) as e:
|
||||||
return Response(json.dumps({"error": str(e), "success":"false"}), status=400, content_type='text/plain')
|
return Response(json.dumps({"error": str(e), "device": id, "status": status, "success":"false"}), status=400, content_type='text/plain')
|
||||||
|
|
||||||
@app.route('/api/json/randhex')
|
@app.route('/api/json/randhex')
|
||||||
def randjsonhex():
|
def randjsonhex():
|
||||||
try:
|
try:
|
||||||
|
status = str(qng_wrapper.statusString())
|
||||||
length = int(request.args.get('length'))
|
length = int(request.args.get('length'))
|
||||||
size = int(request.args.get('size'))
|
size = int(request.args.get('size'))
|
||||||
if length < 1:
|
if length < 1:
|
||||||
return Response(json.dumps({"error": 'length must be greater than 0', "success":"false"}), status=400, content_type='text/plain')
|
return Response(json.dumps({"error": 'length must be greater than 0', "success":"false"}), status=400, content_type='text/plain')
|
||||||
if size < 1:
|
if size < 1:
|
||||||
return Response(json.dumps({"error": 'size must be greater than 0', "success":"false"}), status=400, content_type='text/plain')
|
return Response(json.dumps({"error": 'size must be greater than 0', "success":"false"}), status=400, content_type='text/plain')
|
||||||
|
# entropy = qng_wrapper.randbytes(size*length)
|
||||||
hexarray = []
|
hexarray = []
|
||||||
for x in range(0, length):
|
for x in range(0, length):
|
||||||
hexarray.append(qng_wrapper.randbytes(size).hex())
|
hexarray.append(qng_wrapper.randbytes(size).hex())
|
||||||
return Response(json.dumps({"server" : servername, "device": id, "type": "string", "format": "hex", "length":length, "size": size, "data": hexarray, "success": "true"}), content_type='text/plain')
|
# hexarray.append(entropy[x*size:(x+1)*size].hex())
|
||||||
|
return Response(json.dumps({"server" : servername, "device": id, "status": status, "type": "string", "format": "hex", "length":length, "size": size, "data": hexarray, "success": "true"}), content_type='text/plain')
|
||||||
except (TypeError, ValueError) as e:
|
except (TypeError, ValueError) as e:
|
||||||
return Response(json.dumps({"error": str(e), "success":"false"}), status=400, content_type='text/plain')
|
return Response(json.dumps({"error": str(e), "device": id, "status": status, "success":"false"}), status=400, content_type='text/plain')
|
||||||
|
|
||||||
|
@app.route('/api/json/randbase64')
|
||||||
|
def randjsonbase64():
|
||||||
|
try:
|
||||||
|
status = str(qng_wrapper.statusString())
|
||||||
|
length = int(request.args.get('length'))
|
||||||
|
size = int(request.args.get('size'))
|
||||||
|
if length < 1:
|
||||||
|
return Response(json.dumps({"error": 'length must be greater than 0', "success":"false"}), status=400, content_type='text/plain')
|
||||||
|
if size < 1:
|
||||||
|
return Response(json.dumps({"error": 'size must be greater than 0', "success":"false"}), status=400, content_type='text/plain')
|
||||||
|
# entropy = qng_wrapper.randbytes(size*length)
|
||||||
|
basearray = []
|
||||||
|
for x in range(0, length):
|
||||||
|
basearray.append(base64.b64encode(qng_wrapper.randbytes(size)).decode('utf-8'))
|
||||||
|
# basearray.append(base64.b64encode(entropy[x*size:(x+1)*size]).decode('utf-8'))
|
||||||
|
return Response(json.dumps({"server" : servername, "device": id, "status": status, "type": "string", "format": "base64", "length":length, "size": size, "data": basearray, "success": "true"}), content_type='text/plain')
|
||||||
|
except (TypeError, ValueError) as e:
|
||||||
|
return Response(json.dumps({"error": str(e), "device": id, "status": status, "success":"false"}), status=400, content_type='text/plain')
|
||||||
|
|
||||||
@app.route('/api/clear')
|
|
||||||
def clear():
|
|
||||||
qng_wrapper.clear()
|
|
||||||
return Response(status=204)
|
|
||||||
|
|
||||||
# Websockets ----------------------------------------------
|
# Websockets ----------------------------------------------
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -35,6 +35,8 @@ class QngWrapperLinux:
|
||||||
self._qwqng_wrapper.RandNormal.restype = c_double
|
self._qwqng_wrapper.RandNormal.restype = c_double
|
||||||
self._qwqng_wrapper.RandBytes.argtypes = [c_void_p, c_int]
|
self._qwqng_wrapper.RandBytes.argtypes = [c_void_p, c_int]
|
||||||
self._qwqng_wrapper.RandBytes.restype = POINTER(c_char)
|
self._qwqng_wrapper.RandBytes.restype = POINTER(c_char)
|
||||||
|
self._qwqng_wrapper.StatusString.argtypes = [c_void_p]
|
||||||
|
self._qwqng_wrapper.StatusString.restype = POINTER(c_char)
|
||||||
self._qwqng_wrapper.DeviceID.argtypes = [c_void_p]
|
self._qwqng_wrapper.DeviceID.argtypes = [c_void_p]
|
||||||
self._qwqng_wrapper.DeviceID.restype = POINTER(c_char)
|
self._qwqng_wrapper.DeviceID.restype = POINTER(c_char)
|
||||||
self._qwqng_wrapper.Clear.argtypes = [c_void_p]
|
self._qwqng_wrapper.Clear.argtypes = [c_void_p]
|
||||||
|
|
@ -48,6 +50,13 @@ class QngWrapperLinux:
|
||||||
self._qwqng_wrapper.Reset(self._qng_pointer)
|
self._qwqng_wrapper.Reset(self._qng_pointer)
|
||||||
return cast(self._qwqng_wrapper.DeviceID(self._qng_pointer), c_char_p).value.decode("utf-8")
|
return cast(self._qwqng_wrapper.DeviceID(self._qng_pointer), c_char_p).value.decode("utf-8")
|
||||||
|
|
||||||
|
def statusString(self):
|
||||||
|
try:
|
||||||
|
return cast(self._qwqng_wrapper.StatusString(self._qng_pointer), c_char_p).value.decode("utf-8")
|
||||||
|
except:
|
||||||
|
self._qwqng_wrapper.Reset(self._qng_pointer)
|
||||||
|
return cast(self._qwqng_wrapper.StatusString(self._qng_pointer), c_char_p).value.decode("utf-8")
|
||||||
|
|
||||||
def randint32(self):
|
def randint32(self):
|
||||||
try:
|
try:
|
||||||
return self._qwqng_wrapper.RandInt32(self._qng_pointer)
|
return self._qwqng_wrapper.RandInt32(self._qng_pointer)
|
||||||
|
|
@ -77,16 +86,14 @@ class QngWrapperLinux:
|
||||||
return self._randbytes_arbitrary_length(length)
|
return self._randbytes_arbitrary_length(length)
|
||||||
|
|
||||||
def _randbytes_arbitrary_length(self, length):
|
def _randbytes_arbitrary_length(self, length):
|
||||||
if length <= 8192:
|
try:
|
||||||
return self._qwqng_wrapper.RandBytes(self._qng_pointer, length)[:length]
|
return self._qwqng_wrapper.RandBytes(self._qng_pointer, length)[:length]
|
||||||
else:
|
except:
|
||||||
data = bytearray()
|
self._qwqng_wrapper.Reset(self._qng_pointer)
|
||||||
for x in range(length // 8192):
|
|
||||||
data.extend(self._qwqng_wrapper.RandBytes(self._qng_pointer, 8192)[:8192])
|
|
||||||
bytes_needed = length % 8192
|
|
||||||
if bytes_needed != 0:
|
|
||||||
data.extend(self._qwqng_wrapper.RandBytes(self._qng_pointer, bytes_needed)[:bytes_needed])
|
|
||||||
return bytes(data)
|
|
||||||
|
|
||||||
def clear(self):
|
def clear(self):
|
||||||
self._qwqng_wrapper.Clear(self._qng_pointer)
|
self._qwqng_wrapper.Clear(self._qng_pointer)
|
||||||
|
|
||||||
|
def reset(self):
|
||||||
|
self._qwqng_wrapper.Reset(self._qng_pointer)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue