def make_encrypted(self, compress=False): data = np.arange(2**16, dtype=np.uint16).tobytes() fh = io.BytesIO(F1) fw = datafile.DataFileWriter(fh) signing_key_prv = bytes(range(32)) encryption_key = bytes(range(1, 33)) nonce = bytes(range(16, 16 + 24 * 2, 2)) associated_data = bytes(range(2, 50, 2)) fw.append_encrypted(b'RAW', data, signing_key_prv, encryption_key, nonce, associated_data, compress=compress) fw.finalize() fh.seek(0) fr = datafile.DataFileReader(fh) return fr, { 'data': data, 'signing_key_prv': signing_key_prv, 'signing_key_pub': monocypher.compute_signing_public_key(signing_key_prv), 'encryption_key': encryption_key, 'nonce': nonce, 'associated_data': associated_data, 'fh': fh, }
def signature_start(self, private_key, flags=None): self._signature = { 'flags': 0 if flags is None else int(flags), 'key': private_key, 'data': b'', } payload = bytearray(8) payload[0] = 1 # Ed25519 using Blake2b payload[1] = self._signature['flags'] payload += monocypher.compute_signing_public_key(private_key) self.append(TAG_SIGNATURE_START, payload) if 0 == (self._signature['flags'] & SIGNATURE_FLAG_KEY_INCLUDE): self._signature['data'] = b''
def test_sign(self): random = np.random.RandomState(seed=1) for i in range(10): length = random.randint(1, 4096) secret_key = bytes(random.randint(0, 256, 32, dtype=np.uint8)) msg = bytes(random.randint(0, 256, length, dtype=np.uint8)) public_key = monocypher.compute_signing_public_key(secret_key) sig = monocypher.signature_sign(secret_key, msg) self.assertTrue(monocypher.signature_check(sig, public_key, msg)) self.assertFalse( monocypher.signature_check(sig, public_key, msg + b'0')) sig2 = sig[:10] + bytes([sig[10] + 1]) + sig[11:] self.assertFalse(monocypher.signature_check(sig2, public_key, msg))
CAL1_VG = [0.0009468113102263853, 0.00029342128123136175] CAL1_IK = [ -2806.717811538373, -2822.6645675137756, -2820.88100055503, -2822.6200621623125, -2820.462616626318, -2820.5826040862453, -2818.676888822763, 0.0, float('nan') ] CAL1_1G = [ 0.0006828914450133605, 0.00016375303534373303, 1.4626174421742075e-05, 1.4937616926812478e-06, 1.4970493483159016e-07, 1.498388811827842e-08, 1.4984542304875239e-09, 0.0, float('nan') ] PRIVATE_KEY = bytes(range(32)) PUBLIC_KEY = monocypher.compute_signing_public_key(PRIVATE_KEY) class TestCalibrate(unittest.TestCase): def test_save_load_unsigned(self): c1 = Calibration() c1.current_offset = CAL1_IK[:7] c1.current_gain = CAL1_1G[:7] c1.voltage_offset = CAL1_VK c1.voltage_gain = CAL1_VG data = c1.save() c2 = Calibration().load(data, keys=[]) self.assertFalse(c2.signed) np.testing.assert_allclose(CAL1_VK, c2.voltage_offset) np.testing.assert_allclose(CAL1_VG, c2.voltage_gain) np.testing.assert_allclose(CAL1_IK, c2.current_offset)