mirror of
https://codeberg.org/scip/pcp.git
synced 2025-12-16 19:40:57 +01:00
py: added doc, unittests, anonymous encryption mode
This commit is contained in:
133
tests/pytest.py
133
tests/pytest.py
@@ -1,60 +1,119 @@
|
|||||||
#!/usr/local/bin/python
|
#!/usr/bin/env python
|
||||||
|
|
||||||
from sys import argv, stdout
|
#
|
||||||
from pypcp import *
|
# This file is part of Pretty Curved Privacy (pcp1).
|
||||||
|
#
|
||||||
|
# Copyright (C) 2013-2015 T. von Dein.
|
||||||
|
#
|
||||||
|
# This program is free software: you can redistribute it and/or modify
|
||||||
|
# it under the terms of the GNU General Public License as published by
|
||||||
|
# the Free Software Foundation, either version 3 of the License, or
|
||||||
|
# (at your option) any later version.
|
||||||
|
#
|
||||||
|
# This program is distributed in the hope that it will be useful,
|
||||||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
# GNU General Public License for more details.
|
||||||
|
#
|
||||||
|
# You should have received a copy of the GNU General Public License
|
||||||
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
#
|
||||||
|
# You can contact me by mail: <tlinden AT cpan DOT org>.
|
||||||
|
#
|
||||||
|
|
||||||
|
import sys, os
|
||||||
from pprint import pprint
|
from pprint import pprint
|
||||||
|
|
||||||
|
os.environ['PCPCP_MAKE_TEST'] = "1"
|
||||||
|
sys.path.append('../bindings/py')
|
||||||
|
from pypcp import *
|
||||||
|
|
||||||
orig = "hello world"
|
orig = "hello world"
|
||||||
|
Ali = None
|
||||||
|
Bob = None
|
||||||
|
|
||||||
|
|
||||||
def importkey(filename, passwd=None, secret=True, public=False):
|
class Peer(object):
|
||||||
raw = open(filename, "r").read()
|
def __init__(self, sec, pub, passwd):
|
||||||
if secret and not public:
|
self.sec = 0
|
||||||
key = Key(encoded=raw, passphrase=passwd)
|
self.pub = 0
|
||||||
return key
|
self.ctx = Context()
|
||||||
else:
|
|
||||||
key = PublicKey(encoded=raw)
|
|
||||||
return key
|
|
||||||
|
|
||||||
def asym():
|
if sec:
|
||||||
# import keys
|
self.sec = self.importkey(sec, passwd)
|
||||||
bobSec = importkey("../../tests/key-bobby-sec", "b")
|
self.ctx.addkey(self.sec)
|
||||||
bobPub = importkey("../../tests/key-bobby-pub", public=True)
|
if pub:
|
||||||
aliSec = importkey("../../tests/key-alicia-sec", "a")
|
self.pub = self.importkey(pub, public=True)
|
||||||
aliPub = importkey("../../tests/key-alicia-pub", public=True)
|
self.ctx.recipients(self.pub)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# one context for each
|
def importkey(self, filename, passwd=None, secret=True, public=False):
|
||||||
ctxA = Context()
|
raw = open(filename, "r").read()
|
||||||
ctxB = Context()
|
if secret and not public:
|
||||||
|
key = Key(encoded=raw, passphrase=passwd)
|
||||||
|
return key
|
||||||
|
else:
|
||||||
|
key = PublicKey(encoded=raw)
|
||||||
|
return key
|
||||||
|
|
||||||
# prepare ctx' for crypto
|
|
||||||
ctxA.addkey(aliSec)
|
|
||||||
ctxA.recipients(bobPub)
|
|
||||||
|
|
||||||
ctxB.addkey(bobSec)
|
def impkeys():
|
||||||
ctxB.recipients(aliPub)
|
global Ali
|
||||||
|
global Bob
|
||||||
|
Ali = Peer("key-alicia-sec", "key-bobby-pub", "a")
|
||||||
|
Bob = Peer("key-bobby-sec", "key-alicia-pub", "b")
|
||||||
|
|
||||||
|
# no exception here - done
|
||||||
|
return True
|
||||||
|
|
||||||
|
def asym(armor=True, sign=False):
|
||||||
|
if Ali is None:
|
||||||
|
impkeys()
|
||||||
|
|
||||||
# Alice encrypt => Bob
|
# Alice encrypt => Bob
|
||||||
# FIXME: if passed as is, then it's empty later on
|
encrypted = Ali.ctx.encrypt(source=orig, armor=armor, sign=sign)
|
||||||
encrypted = ctxA.encrypt(source="%s" % orig, armor=True)
|
|
||||||
#print "encrypted:\n%s" % encrypted
|
|
||||||
|
|
||||||
# Bob decrypt from Alice
|
# Bob decrypt from Alice
|
||||||
clear = ctxB.decrypt(source=encrypted)
|
clear = Bob.ctx.decrypt(source=encrypted, verify=sign)
|
||||||
#print "clear: %s" % clear
|
|
||||||
|
|
||||||
if clear == orig:
|
if clear == orig:
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def asymarmor():
|
||||||
|
return asym(armor=True)
|
||||||
|
|
||||||
def sym():
|
def asymraw():
|
||||||
|
return asym(armor=False)
|
||||||
|
|
||||||
|
def asymsign():
|
||||||
|
return asym(sign=True)
|
||||||
|
|
||||||
|
def asymanon():
|
||||||
|
Ali = Peer(None, "key-bobby-pub", None)
|
||||||
|
Bob = Peer("key-bobby-sec", None, "b")
|
||||||
|
|
||||||
|
# Anon encrypt => Bob
|
||||||
|
encrypted = Ali.ctx.encrypt(source=orig)
|
||||||
|
|
||||||
|
# Bob decrypt from Anon
|
||||||
|
clear = Bob.ctx.decrypt(source=encrypted)
|
||||||
|
|
||||||
|
if clear == orig:
|
||||||
|
return True
|
||||||
|
else:
|
||||||
|
return False
|
||||||
|
|
||||||
|
return True
|
||||||
|
|
||||||
|
def sym(armor=True):
|
||||||
# always required
|
# always required
|
||||||
ctx = Context()
|
ctx = Context()
|
||||||
|
|
||||||
# symmetric encryption (self mode)
|
# symmetric encryption (self mode)
|
||||||
encrypted = ctx.encrypt(source="%s" % orig, passphrase="x", armor=True)
|
encrypted = ctx.encrypt(source=orig, passphrase="x", armor=armor)
|
||||||
|
|
||||||
# decrypt
|
# decrypt
|
||||||
clear = ctx.decrypt(source=encrypted, passphrase="x")
|
clear = ctx.decrypt(source=encrypted, passphrase="x")
|
||||||
@@ -64,7 +123,11 @@ def sym():
|
|||||||
else:
|
else:
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
def symarmor():
|
||||||
|
return sym(armor=True)
|
||||||
|
|
||||||
|
def symraw():
|
||||||
|
return sym(armor=False)
|
||||||
|
|
||||||
def defun(name):
|
def defun(name):
|
||||||
if not globals()[name]():
|
if not globals()[name]():
|
||||||
@@ -75,12 +138,12 @@ def defun(name):
|
|||||||
return True
|
return True
|
||||||
|
|
||||||
|
|
||||||
if len(argv) == 2:
|
if len(sys.argv) == 2:
|
||||||
if defun(argv[1]):
|
if defun(sys.argv[1]):
|
||||||
exit(0)
|
exit(0)
|
||||||
else:
|
else:
|
||||||
# execute all
|
# execute all
|
||||||
for func in ["asym", "sym"]:
|
for func in ["impkeys", "asymarmor", "asymraw", "asymsign", "symarmor", "symraw"]:
|
||||||
defun(func)
|
defun(func)
|
||||||
exit(0)
|
exit(0)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user