1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
|
# -*- coding: utf-8 -*-
from datetime import datetime
from struct import pack, unpack
from Crypto.Cipher import Blowfish
import binascii
class Entry:
ENTRY_SIZE = 22
DATA_SIZE = 12
def __init__(self, timestamp=0, data="", entete = "", signal=0):
'''
@param timestamp: the date of reception of the data
@param data: data received from Sigfox
@param signal: the strangth of the signal, 0 means unknown
'''
self.time = datetime.fromtimestamp(timestamp)
self.pressure = 0
self.temperature = 0
self.humidity = 0
self.external = 0
self.external2 = 0
self.external3 = 0
self.data = data
self.entete = entete
self.signal = signal
def init(self, time, p, t, h, e, signal):
self.data = ""
self.time = datetime.fromtimestamp(time)
self.pressure = p
self.temperature = t
self.humidity = h
self.external = e
self.signal = signal
def init2(self, time, p, t, h, e, e2, signal):
self.data = ""
self.time = datetime.fromtimestamp(time)
self.pressure = p
self.temperature = t
self.humidity = h
self.external = e
self.external2 = e2
self.signal = signal
#Partie de chiffrement : BlowFish
#Variable glocal c1 et key
def PKCS5Padding(string):
byteNum = len(string)
packingLength = 8 - byteNum % 8
appendage = chr(packingLength) * packingLength
return string + appendage
def PandoraEncrypt(string):
key = b'6#26FRL$ZWD'
c1 = Blowfish.new(key, Blowfish.MODE_ECB)
packedString = PKCS5Padding(string)
return c1.encrypt(packedString)
def PandoraDecrypt(string):
key = b'6#26FRL$ZWD'
c1 = Blowfish.new(key, Blowfish.MODE_ECB)
return c1.decrypt(string)
if __name__ == '__main__':
s = 'testings'
data_encrypted= PandoraEncrypt(s)
data_decrypted = PandoraDecrypt(s)
print(binascii.hexlify(data_encrypted))
print(binascii.hexlify(data_decrypted))
assert data_decrypted == s
def pack(self):
'''
@pre : L'année en cours est inférieur à 2106 (timestamp sur 32 bits) et data fait 12 chars
@post : On renvoie 20 chars
@note : Format : 5 bits p -- 6 bits t -- 5 bits h -- 32 bits e, en hexadécimal
'''
assert len(self.data) == Entry.DATA_SIZE, "Entry data should be 12 char long."
entryData = "%8s" % hex(int(self.time.strftime("%s")))[2:].zfill(8)+self.data+hex(int(self.signal*2.5))[2:].zfill(2)
assert len(entryData) == Entry.ENTRY_SIZE, "Packed entry data should be 22 char long and not %d" % len(entryData)
return entryData |
Partager