| 1 | """This is a playground to develop the api for the pubkey and secret key. |
|---|
| 2 | Don't take the crypto seriously""" |
|---|
| 3 | from containerbase import * |
|---|
| 4 | import hashlib |
|---|
| 5 | import rsa |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | class KeyField(Field): |
|---|
| 10 | |
|---|
| 11 | def __init__(self,name,signing=True,isPrivate=False,default=''): |
|---|
| 12 | Field.__init__(self,name=name,signing=signing,default=default) |
|---|
| 13 | self.isPrivate = isPrivate |
|---|
| 14 | |
|---|
| 15 | def getencoded(self,object,allData=False): |
|---|
| 16 | value = getattr(object,self.name,self.default) |
|---|
| 17 | items = value.items() |
|---|
| 18 | items.sort() |
|---|
| 19 | #items = [(key,hex(val)) for key,val in items] |
|---|
| 20 | return items |
|---|
| 21 | |
|---|
| 22 | def setdecoded(self,object,data): |
|---|
| 23 | #data = [(key,int(val,0)) for key,val in data] |
|---|
| 24 | setattr(object,self.name,dict(data)) |
|---|
| 25 | |
|---|
| 26 | class PubKey(Container): |
|---|
| 27 | |
|---|
| 28 | fields = [KeyField('key')] |
|---|
| 29 | |
|---|
| 30 | def encrypt(self,data): |
|---|
| 31 | return rsa.encrypt(data,self.key) |
|---|
| 32 | |
|---|
| 33 | def verifySignature(self,signature,data): |
|---|
| 34 | clear = rsa.verify(signature,self.key) |
|---|
| 35 | return data == clear |
|---|
| 36 | |
|---|
| 37 | def verifyContainerSignature(self,container): |
|---|
| 38 | signature = container.signature |
|---|
| 39 | data = container.hash() |
|---|
| 40 | return self.verifySignature(signature=signature,data=data) |
|---|
| 41 | |
|---|
| 42 | def createBlindingSecret(self): |
|---|
| 43 | return rsa.getUnblinder(self.key['n']) |
|---|
| 44 | |
|---|
| 45 | def blind(self,data): |
|---|
| 46 | secret = self.createBlindingSecret() |
|---|
| 47 | blinder = pow(rsa.invMod(secret, self.key['n']), self.key['e'], self.key['n']) |
|---|
| 48 | blinded = rsa.blind(data,blinder,self.key) |
|---|
| 49 | return secret, blinded |
|---|
| 50 | |
|---|
| 51 | def blindBlank(self,blank): |
|---|
| 52 | return self.blind(blank.hash()) |
|---|
| 53 | |
|---|
| 54 | def unblind(self,secret,data): |
|---|
| 55 | number = data |
|---|
| 56 | return (number * secret) % self.key['n'] |
|---|
| 57 | |
|---|
| 58 | class PrivKey(Container): |
|---|
| 59 | |
|---|
| 60 | fields = [KeyField('key')] |
|---|
| 61 | |
|---|
| 62 | def decrypt(self,data): |
|---|
| 63 | return rsa.decrypt(data,self.key) |
|---|
| 64 | |
|---|
| 65 | def sign(self,data): |
|---|
| 66 | return rsa.sign(data,self.key) |
|---|
| 67 | |
|---|
| 68 | def signblind(self,number): |
|---|
| 69 | return rsa.encrypt_int(number,self.key['d'],self.key['n']) |
|---|
| 70 | |
|---|
| 71 | def signContainer(self,container): |
|---|
| 72 | signature = self.sign(container.hash()) |
|---|
| 73 | container.signature = signature |
|---|
| 74 | return container |
|---|
| 75 | |
|---|
| 76 | def hash(data): |
|---|
| 77 | return rsa.bytes2int(hashlib.sha256(data).hexdigest()) |
|---|
| 78 | |
|---|
| 79 | def hashContainer(container,allData=False): |
|---|
| 80 | return hash(container.toString(allData=allData)) |
|---|
| 81 | |
|---|
| 82 | def createSerial(): |
|---|
| 83 | return rsa.bytesToNumber(rsa.getRandomBytes(16)) |
|---|
| 84 | #!!! |
|---|
| 85 | Container.hash = hashContainer |
|---|
| 86 | |
|---|
| 87 | def KeyFactory(bitlen): |
|---|
| 88 | pub,priv = rsa.gen_pubpriv_keys(bitlen) |
|---|
| 89 | pubkey = PubKey() |
|---|
| 90 | pubkey.key = pub |
|---|
| 91 | privkey = PrivKey() |
|---|
| 92 | privkey.key = priv |
|---|
| 93 | return privkey,pubkey |
|---|
| 94 | |
|---|
| 95 | if __name__ == '__main__': |
|---|
| 96 | keys = KeyFactory(1024) |
|---|