~anitanayak/charms/trusty/ibm-mq/ibm-mq-trusty

« back to all changes in this revision

Viewing changes to .tox/py35/lib/python3.5/site-packages/wheel/signatures/ed25519py.py

  • Committer: Anita Nayak
  • Date: 2016-10-24 07:10:08 UTC
  • Revision ID: anitanayak@in.ibm.com-20161024071008-tqk3cefak6nc1c69
checking in after fixing lint errors

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
 
 
3
import warnings
 
4
import os
 
5
 
 
6
from collections import namedtuple
 
7
from . import djbec
 
8
 
 
9
__all__ = ['crypto_sign', 'crypto_sign_open', 'crypto_sign_keypair', 'Keypair',
 
10
           'PUBLICKEYBYTES', 'SECRETKEYBYTES', 'SIGNATUREBYTES']
 
11
 
 
12
PUBLICKEYBYTES=32
 
13
SECRETKEYBYTES=64
 
14
SIGNATUREBYTES=64
 
15
 
 
16
Keypair = namedtuple('Keypair', ('vk', 'sk')) # verifying key, secret key
 
17
 
 
18
def crypto_sign_keypair(seed=None):
 
19
    """Return (verifying, secret) key from a given seed, or os.urandom(32)"""    
 
20
    if seed is None:
 
21
        seed = os.urandom(PUBLICKEYBYTES)
 
22
    else:
 
23
        warnings.warn("ed25519ll should choose random seed.",
 
24
                      RuntimeWarning)
 
25
    if len(seed) != 32:
 
26
        raise ValueError("seed must be 32 random bytes or None.")
 
27
    skbytes = seed
 
28
    vkbytes = djbec.publickey(skbytes)
 
29
    return Keypair(vkbytes, skbytes+vkbytes)
 
30
 
 
31
 
 
32
def crypto_sign(msg, sk):
 
33
    """Return signature+message given message and secret key.
 
34
    The signature is the first SIGNATUREBYTES bytes of the return value.
 
35
    A copy of msg is in the remainder."""
 
36
    if len(sk) != SECRETKEYBYTES:
 
37
        raise ValueError("Bad signing key length %d" % len(sk))
 
38
    vkbytes = sk[PUBLICKEYBYTES:]
 
39
    skbytes = sk[:PUBLICKEYBYTES]
 
40
    sig = djbec.signature(msg, skbytes, vkbytes)
 
41
    return sig + msg
 
42
 
 
43
 
 
44
def crypto_sign_open(signed, vk):
 
45
    """Return message given signature+message and the verifying key."""
 
46
    if len(vk) != PUBLICKEYBYTES:
 
47
        raise ValueError("Bad verifying key length %d" % len(vk))
 
48
    rc = djbec.checkvalid(signed[:SIGNATUREBYTES], signed[SIGNATUREBYTES:], vk)
 
49
    if not rc:
 
50
        raise ValueError("rc != True", rc)    
 
51
    return signed[SIGNATUREBYTES:]
 
52