|
| 1 | +import hashlib, time |
| 2 | + |
| 3 | + |
| 4 | +sha_stress_running = None |
| 5 | +sha_stress_verbose = None |
| 6 | + |
| 7 | +def sha_test(lenght=100): |
| 8 | + import machine |
| 9 | + import hashlib |
| 10 | + |
| 11 | + buf = bytearray() |
| 12 | + for x in range(lenght//3): |
| 13 | + buf += machine.rng().to_bytes(3, 'big') |
| 14 | + # print(buf) |
| 15 | + |
| 16 | + h = hashlib.sha256(buf) |
| 17 | + # print(hex(buf[0]), hex(buf[-1]), h.digest()) |
| 18 | + return h.digest() |
| 19 | + |
| 20 | +def sha_stress(verbose=True): |
| 21 | + global sha_stress_running, sha_stress_verbose |
| 22 | + import machine |
| 23 | + l = 20_000 |
| 24 | + sha_stress_running = True |
| 25 | + sha_stress_verbose = verbose |
| 26 | + print('sha_stress start', l) |
| 27 | + while sha_stress_running: |
| 28 | + t_ms = time.ticks_ms() |
| 29 | + d = sha_test(l) |
| 30 | + temp = (machine.temperature()-32)/1.8 |
| 31 | + t_s = (time.ticks_ms()-t_ms)/1000 |
| 32 | + sha_stress_verbose and print(time.time(), t_s, temp, len(d)) |
| 33 | + pybytes.send_signal(1,time.time()) |
| 34 | + pybytes.send_signal(2, temp) |
| 35 | + pybytes.send_signal(3, t_s) |
| 36 | + print('sha_stress end') |
| 37 | + |
| 38 | +def sha_hello(): |
| 39 | + |
| 40 | + t = time.time() |
| 41 | + if t % 2: |
| 42 | + msg = 'asdf' |
| 43 | + else: |
| 44 | + msg = 'jkl' |
| 45 | + h = hashlib.sha256(msg) |
| 46 | + print(msg, h.digest()) |
| 47 | + |
| 48 | + if t % 2: |
| 49 | + msg = 'jkl' |
| 50 | + else: |
| 51 | + msg = 'asdf' |
| 52 | + h = hashlib.sha256(msg) |
| 53 | + # h.update(msg) |
| 54 | + print(msg, h.digest()) |
| 55 | + |
| 56 | +if __name__ == "__main__": |
| 57 | + if False: |
| 58 | + sha_stress() |
| 59 | + else: |
| 60 | + import _thread |
| 61 | + _thread.start_new_thread(sha_stress, (False,)) |
| 62 | + |
| 63 | + if False: |
| 64 | + sha_stress_running = False |
| 65 | + sha_stress_verbose = False |
| 66 | + sha_stress_verbose = True |
| 67 | + pybytes.send_signal(1,time.time()) |
0 commit comments