コード例 #1
0
ファイル: __init__.py プロジェクト: wizardofozzie/pyBIP0038
def aes_decrypt_bip38(encMsg,key,pad='{'):
    """
    Very simple AES decryption, with parameters specifically for
    use in BIP0038.

    # Doctest done this way so it outputs the same for Python 2 and 3
    >>> hexstrlify(aes_decrypt_bip38(unhexlify("8f4b4aa6e27d1669ba5dd6039c16d4f1"),unhexlify("3cfc181482b735941483ec8f158314f9ada2aa0d6e4a5c15bd46515092716d3b")))
    '45e8364d907e802d87d3b29f4b527b49'
    >>> hexstrlify(aes_decrypt_bip38(unhexlify("1e7bddc6f793d4444e61a99f5e57fd44"),unhexlify("3cfc181482b735941483ec8f158314f9ada2aa0d6e4a5c15bd46515092716d3b")))
    '45e8364d907e802d87d3b29f4b7b7b7b'
    """

    pad = pad.encode('utf-8')
    key = hexstrlify(key)
    if len(key) != 64:
        raise Exception('aes_decrypt_bip38() key size must be 32 bytes')
    key = unhexlify(key)
    cipher = AES.new(key)
    msg = cipher.decrypt(encMsg)
    try:
        msg = msg.rstrip(pad)
    except:
        msg = msg.rstrip(bytes(pad,'utf-8'))
    if len(msg) != 16:
        if len(msg) > 16:
            raise Exception('aes_decrypt_bip38() decrypted msg larger than 16 bytes after pad strip')
        else:
            msg = msg + pad * int(16 - len(msg))
    return msg
コード例 #2
0
ファイル: misc_ops.py プロジェクト: AmiZya/emesene
def bytes(text):
    """
    Convert Unicode text to UTF-8 encoded bytes.

    Since Python 2.6+ and Python 3+ have similar but incompatible
    signatures, this function unifies the two to keep code sane.

    :param text: Unicode text to convert to bytes
    :rtype: bytes (Python3), str (Python2.6+)
    """
    if text is None:
        return b''

    if sys.version_info < (3, 0):
        import __builtin__
        return __builtin__.bytes(text)
    else:
        import builtins
        if isinstance(text, builtins.bytes):
            # We already have bytes, so do nothing
            return text
        if isinstance(text, list):
            # Convert a list of integers to bytes
            return builtins.bytes(text)
        else:
            # Convert UTF-8 text to bytes
            return builtins.bytes(text, encoding='utf-8')
コード例 #3
0
ファイル: misc_ops.py プロジェクト: voiser/zoe-startup-kit
def bytes(text):
    """
    Convert Unicode text to UTF-8 encoded bytes.

    Since Python 2.6+ and Python 3+ have similar but incompatible
    signatures, this function unifies the two to keep code sane.

    :param text: Unicode text to convert to bytes
    :rtype: bytes (Python3), str (Python2.6+)
    """
    if text is None:
        return b''

    if sys.version_info < (3, 0):
        import __builtin__
        return __builtin__.bytes(text)
    else:
        import builtins
        if isinstance(text, builtins.bytes):
            # We already have bytes, so do nothing
            return text
        if isinstance(text, list):
            # Convert a list of integers to bytes
            return builtins.bytes(text)
        else:
            # Convert UTF-8 text to bytes
            return builtins.bytes(text, encoding='utf-8')
コード例 #4
0
ファイル: random.py プロジェクト: marasong/pyufp
def bytes(size):
    """
	size byte만큼의 램덤 바이트 데이터를 만듭니다.
	
	.. 참조 : http://stackoverflow.com/questions/5495492/random-byte-string-in-python/28131868#28131868
	
	:param size: 생성할 램덤 바이트 크기(단위 byte).
	:type size: int
	:return: 램덤 바이트 데이터.
	:rtype: bytes
	"""
    buffer = [random.getrandbits(8) for _ in xrange(size)]
    buffer = bytearray(buffer)
    return __builtin__.bytes(buffer)
コード例 #5
0
def save_atomic(filename, content, makedirs=True):
    """
    Save `content` to `filename` atomicaly. This prevents others from
    reading half-written files.

    :param makedirs: create directories if needed
    """
    if makedirs:
        try:
            os.makedirs(os.path.dirname(filename))
        except OSError as ex:
            if ex.errno != errno.EEXIST:
                raise

    tmp_filename = filename + '~'
    with open(tmp_filename, 'wb') as f:
        if PY2:
            f.write(bytes(content))
        else:
            f.write(bytes(content, 'UTF-8'))
        f.flush()
        os.fsync(f.fileno())

    os.rename(tmp_filename, filename)
コード例 #6
0
def force_bytes(s, encoding, errors="strict"):
    """Converts s to bytes, using the provided encoding. If s is already bytes,
    it is returned as is.

    If errors="strict" and s is bytes, its encoding is verified by decoding it;
    UnicodeError is raised if it cannot be decoded.
    """
    if isinstance(s, unicode):
        return s.encode(encoding, errors)
    else:
        s = bytes(s)
        if errors == "strict":
            # Return value ignored - invoked solely for verification.
            s.decode(encoding, errors)
        return s
コード例 #7
0
def save_atomic(filename, content, makedirs=True):
    """
    Save `content` to `filename` atomicaly. This prevents others from
    reading half-written files.

    :param makedirs: create directories if needed
    """
    if makedirs:
        try:
            os.makedirs(os.path.dirname(filename))
        except OSError as ex:
            if ex.errno != errno.EEXIST:
                raise

    tmp_filename = filename + '~'
    with open(tmp_filename, 'wb') as f:
        if PY2:
            f.write(bytes(content))
        else:
            f.write(bytes(content, 'UTF-8'))
        f.flush()
        os.fsync(f.fileno())

    os.rename(tmp_filename, filename)
コード例 #8
0
 def bytes(object, unused=None):
     return __builtin__.bytes(object)
コード例 #9
0
 def bytes(object, unused = None):
     return __builtin__.bytes(object)
コード例 #10
0
 def bytes(string, encoding):
     return __builtin__.bytes(string)
コード例 #11
0
ファイル: testCommunicate.py プロジェクト: El-gitano/otr_new
 def bytes(s, encoding='ascii'):
     return __builtin__.bytes(s)