Example #1
0
 def __init__(self, pubkey=None, privkey=None):
     if not pubkey or not privkey:
         keypair = GenerateKeyPair(KEY_PAIR_TYPE.EC)
         privkey = keypair.export_private_key()
         pubkey = keypair.export_public_key()
     self.pubkey = pubkey
     self.privkey = privkey
     self.privkey_obj = ComradAsymmetricPrivateKey(privkey, pubkey)
     self.pubkey_obj = ComradAsymmetricPublicKey(pubkey, privkey)
Example #2
0
def create_acrastruct(data, acra_public_key, context=None):
    random_kp = GenerateKeyPair(KEY_PAIR_TYPE.EC)
    smessage = SMessage(random_kp.export_private_key(), acra_public_key)
    random_key = generate_key()
    wrapped_random_key = smessage.wrap(random_key)

    scell = SCellSeal(random_key)
    encrypted_data = scell.encrypt(data, context)
    del random_key
    encrypted_data_len = struct.pack('<Q', len(encrypted_data))

    acrastruct = (BEGIN_TAG + random_kp.export_public_key() +
                  wrapped_random_key + encrypted_data_len + encrypted_data)
    del random_kp
    del wrapped_random_key
    return acrastruct
Example #3
0
    def test_generator(self):
        ec_keypair = GenerateKeyPair(KEY_PAIR_TYPE.EC)
        self.assertTrue(ec_keypair.export_private_key())
        self.assertTrue(ec_keypair.export_public_key())

        rsa_keypair = GenerateKeyPair(KEY_PAIR_TYPE.RSA)
        self.assertTrue(rsa_keypair.export_private_key())
        self.assertTrue(rsa_keypair.export_public_key())

        self.assertNotEqual(rsa_keypair.export_private_key(),
                            ec_keypair.export_private_key())

        self.assertNotEqual(rsa_keypair.export_public_key(),
                            ec_keypair.export_public_key())

        with self.assertRaises(ThemisError):
            GenerateKeyPair("incorrect algorithm")
Example #4
0
    def test_generator(self):
        ec_keypair = GenerateKeyPair(KEY_PAIR_TYPE.EC)
        self.assertTrue(ec_keypair.export_private_key())
        self.assertTrue(ec_keypair.export_public_key())

        rsa_keypair = GenerateKeyPair(KEY_PAIR_TYPE.RSA)
        self.assertTrue(rsa_keypair.export_private_key())
        self.assertTrue(rsa_keypair.export_public_key())

        self.assertNotEqual(rsa_keypair.export_private_key(),
                            ec_keypair.export_private_key())

        self.assertNotEqual(rsa_keypair.export_public_key(),
                            ec_keypair.export_public_key())

        with self.assertRaises(ThemisError):
            GenerateKeyPair("incorrect algorithm")
Example #5
0
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import sys
import os
from pythemis.skeygen import KEY_PAIR_TYPE, GenerateKeyPair

if len(sys.argv) != 3:
    print("Usage: python {} <private_key_path> <public_key_path>".format(
        os.path.basename(__file__)))
    exit(1)

keypair = GenerateKeyPair(KEY_PAIR_TYPE.EC)
private_key = keypair.export_private_key()
public_key = keypair.export_public_key()

with open(sys.argv[1], "wb") as private_key_file:
    print("Save private key to {}".format(sys.argv[1]))
    private_key_file.write(private_key)

with open(sys.argv[2], "wb") as public_key_file:
    print("Save public key to {}".format(sys.argv[2]))
    public_key_file.write(public_key)

Example #6
0
 def testWithEncoding(self):
     test_data = 'some data'
     public_key = GenerateKeyPair(KEY_PAIR_TYPE.EC).export_public_key()
     self.assertIsNotNone(create_acrastruct(test_data, public_key))
Example #7
0
 def testWithContext(self):
     test_data = b'some data'
     context = b'some context'
     public_key = GenerateKeyPair(KEY_PAIR_TYPE.EC).export_public_key()
     self.assertIsNotNone(create_acrastruct(test_data, public_key, context))