本文整理汇总了Python中base64.b16encode方法的典型用法代码示例。如果您正苦于以下问题:Python base64.b16encode方法的具体用法?Python base64.b16encode怎么用?Python base64.b16encode使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类base64
的用法示例。
在下文中一共展示了base64.b16encode方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _filter_encode
# 需要导入模块: import base64 [as 别名]
# 或者: from base64 import b16encode [as 别名]
def _filter_encode(self, data, encoding):
if its.py_v3 and isinstance(data, str):
data = data.encode('utf-8')
encoding = encoding.lower()
encoding = re.sub(r'^(base|rot)-(\d\d)$', r'\1\2', encoding)
if encoding == 'base16' or encoding == 'hex':
data = base64.b16encode(data)
elif encoding == 'base32':
data = base64.b32encode(data)
elif encoding == 'base64':
data = base64.b64encode(data)
elif encoding == 'rot13':
data = codecs.getencoder('rot-13')(data.decode('utf-8'))[0]
else:
raise ValueError('Unknown encoding type: ' + encoding)
if its.py_v3 and isinstance(data, bytes):
data = data.decode('utf-8')
return data
示例2: torrent
# 需要导入模块: import base64 [as 别名]
# 或者: from base64 import b16encode [as 别名]
def torrent(self):
""":class:`Torrent` instance"""
# Prevent circular import issues
from ._torrent import Torrent
torrent = Torrent()
torrent.name = self.dn
if self.tr:
torrent.trackers = self.tr
if self.ws:
torrent.webseeds = self.ws
if self.xl:
torrent._metainfo['info']['length'] = self.xl
if hasattr(self, '_info'):
torrent.metainfo['info'] = self._info
elif len(self.infohash) == 40:
torrent._infohash = self.infohash
else:
# Convert base 32 to base 16 (SHA1)
torrent._infohash = base64.b16encode(
base64.b32decode(self.infohash)).decode('utf-8').lower()
return torrent
示例3: crypter
# 需要导入模块: import base64 [as 别名]
# 或者: from base64 import b16encode [as 别名]
def crypter(ack='',iv='2769514380123456',base='b64'):
ahk = hmac.new(b'vilame',ack.encode(),'md5').hexdigest()
c = CrypterAES(ahk, iv)
if base == 'b16': _encode,_decode = base64.b16encode,base64.b16decode
if base == 'b32': _encode,_decode = base64.b32encode,base64.b32decode
if base == 'b64': _encode,_decode = base64.b64encode,base64.b64decode
if base == 'b85': _encode,_decode = base64.b85encode,base64.b85decode
if base == 'urlsafe_b64': _encode,_decode = base64.urlsafe_b64encode,base64.urlsafe_b64decode
def zbase_enc(data):
return _encode(zlib.compress(data.encode())[2:-4]).decode()
def zbase_dec(basedata):
return zlib.decompress(_decode(basedata),-15).decode()
def zencrypt(data): return c.encrypt_base(zlib.compress(data.encode())[2:-4],_encode)
def zdecrypt(data): return zlib.decompress(c.decrypt_base(data,_decode),-15).decode()
c.zencrypt = zencrypt
c.zdecrypt = zdecrypt
c.zbase_enc = zbase_enc
c.zbase_dec = zbase_dec
c.encrypt = lambda data:c.encrypt_base(data,_encode)
c.decrypt = lambda data:c.decrypt_base(data,_decode).decode()
return c
示例4: dnshostencode
# 需要导入模块: import base64 [as 别名]
# 或者: from base64 import b16encode [as 别名]
def dnshostencode(data, zone):
"""
encodes the data in a DNS transmittable hostname, 0-9A-F
:param data: DNS transmittable hostname data
:param zone: DNS zone to add at the end
:return: encoded form
"""
# TODO: sending 0-9A-Z would be better
res = b""
sdata = base64.b16encode(data)
# every 60 characters, we will add a dot
for i in range(len(sdata)):
res += sdata[i:i+1]
if (i+1) % 60 == 0 and (i+1) < len(sdata):
res += b'.'
return res + b'.' + zone.encode('utf-8') + b'.'
示例5: dnsip6encode
# 需要导入模块: import base64 [as 别名]
# 或者: from base64 import b16encode [as 别名]
def dnsip6encode(data):
"""
encodes the data as a single IPv6 address
:param data: data to encode
:return: encoded form
"""
if len(data) != 16:
print_error("dnsip6encode: data is more or less than 16 bytes, cannot encode")
return None
res = b''
reslen = 0
for i in range(len(data)):
res += base64.b16encode(data[i:i+1])
reslen += 1
if reslen % 2 == 0:
res += b':'
return res[:-1]
示例6: toHex
# 需要导入模块: import base64 [as 别名]
# 或者: from base64 import b16encode [as 别名]
def toHex(data, maxLen=0, upperCase=True, blockSize=0):
if (type(data) == str):
data = bytes(data,'utf-8')
if maxLen:
data = data[:maxLen]
if blockSize:
dataBlocks = split(data, blockSize, False)
else:
dataBlocks = [data]
hex=' '.join([str(base64.b16encode(b),'utf-8') for b in dataBlocks])
if not upperCase:
hex= hex.lower()
return hex
示例7: encode
# 需要导入模块: import base64 [as 别名]
# 或者: from base64 import b16encode [as 别名]
def encode(self, text):
return base64.b16encode(text).lower()
示例8: convertColormapToPalette
# 需要导入模块: import base64 [as 别名]
# 或者: from base64 import b16encode [as 别名]
def convertColormapToPalette(name):
cmap = matplotlib.cm.get_cmap(name)
norm = matplotlib.colors.Normalize(vmin = 0,vmax = 255)
rgb = []
for i in range(0, 255):
k = matplotlib.colors.colorConverter.to_rgb(cmap(norm(i)))
rgb.append(k)
entries = 255
h = 1.0 / (entries - 1)
colorscale = []
prev = None
for k in range(entries):
c = list(map(np.uint8,np.array(cmap(k * h)[:3]) * 255))
value = (c[0],c[1],c[2])
if value == prev:
continue
prev = value
s = "#" + b16encode(bytes(value)).decode()
colorscale.append(s)
return(colorscale)
示例9: _temporary_keychain
# 需要导入模块: import base64 [as 别名]
# 或者: from base64 import b16encode [as 别名]
def _temporary_keychain():
"""
This function creates a temporary Mac keychain that we can use to work with
credentials. This keychain uses a one-time password and a temporary file to
store the data. We expect to have one keychain per socket. The returned
SecKeychainRef must be freed by the caller, including calling
SecKeychainDelete.
Returns a tuple of the SecKeychainRef and the path to the temporary
directory that contains it.
"""
# Unfortunately, SecKeychainCreate requires a path to a keychain. This
# means we cannot use mkstemp to use a generic temporary file. Instead,
# we're going to create a temporary directory and a filename to use there.
# This filename will be 8 random bytes expanded into base64. We also need
# some random bytes to password-protect the keychain we're creating, so we
# ask for 40 random bytes.
random_bytes = os.urandom(40)
filename = base64.b16encode(random_bytes[:8]).decode('utf-8')
password = base64.b16encode(random_bytes[8:]) # Must be valid UTF-8
tempdirectory = tempfile.mkdtemp()
keychain_path = os.path.join(tempdirectory, filename).encode('utf-8')
# We now want to create the keychain itself.
keychain = Security.SecKeychainRef()
status = Security.SecKeychainCreate(
keychain_path,
len(password),
password,
False,
None,
ctypes.byref(keychain)
)
_assert_no_error(status)
# Having created the keychain, we want to pass it off to the caller.
return keychain, tempdirectory
示例10: _temporary_keychain
# 需要导入模块: import base64 [as 别名]
# 或者: from base64 import b16encode [as 别名]
def _temporary_keychain():
"""
This function creates a temporary Mac keychain that we can use to work with
credentials. This keychain uses a one-time password and a temporary file to
store the data. We expect to have one keychain per socket. The returned
SecKeychainRef must be freed by the caller, including calling
SecKeychainDelete.
Returns a tuple of the SecKeychainRef and the path to the temporary
directory that contains it.
"""
# Unfortunately, SecKeychainCreate requires a path to a keychain. This
# means we cannot use mkstemp to use a generic temporary file. Instead,
# we're going to create a temporary directory and a filename to use there.
# This filename will be 8 random bytes expanded into base64. We also need
# some random bytes to password-protect the keychain we're creating, so we
# ask for 40 random bytes.
random_bytes = os.urandom(40)
filename = base64.b16encode(random_bytes[:8]).decode("utf-8")
password = base64.b16encode(random_bytes[8:]) # Must be valid UTF-8
tempdirectory = tempfile.mkdtemp()
keychain_path = os.path.join(tempdirectory, filename).encode("utf-8")
# We now want to create the keychain itself.
keychain = Security.SecKeychainRef()
status = Security.SecKeychainCreate(
keychain_path, len(password), password, False, None, ctypes.byref(keychain)
)
_assert_no_error(status)
# Having created the keychain, we want to pass it off to the caller.
return keychain, tempdirectory
示例11: binary_to_snowflake
# 需要导入模块: import base64 [as 别名]
# 或者: from base64 import b16encode [as 别名]
def binary_to_snowflake(binary_value):
"""Encodes a "bytes" object for passing to Snowflake."""
result = b16encode(binary_value)
if isinstance(binary_value, bytearray):
return bytearray(result)
return result
示例12: __init__
# 需要导入模块: import base64 [as 别名]
# 或者: from base64 import b16encode [as 别名]
def __init__(self, name):
name = name.upper()
if name == 'HEX':
self._encode = b16encode
elif name == 'BASE64':
self._encode = standard_b64encode
else:
raise InternalError(
'Unrecognized binary format {}'.format(name))
示例13: encrypt
# 需要导入模块: import base64 [as 别名]
# 或者: from base64 import b16encode [as 别名]
def encrypt():
cipher = AES.new(base64.b16decode(key, casefold=True), AES.MODE_ECB)
return base64.b16encode(cipher.encrypt(flag))
# flush output immediately
示例14: test_b16encode
# 需要导入模块: import base64 [as 别名]
# 或者: from base64 import b16encode [as 别名]
def test_b16encode(self):
eq = self.assertEqual
eq(base64.b16encode('\x01\x02\xab\xcd\xef'), '0102ABCDEF')
eq(base64.b16encode('\x00'), '00')
# Non-bytes
eq(base64.b16encode(bytearray('\x01\x02\xab\xcd\xef')), '0102ABCDEF')
示例15: _base16_encode
# 需要导入模块: import base64 [as 别名]
# 或者: from base64 import b16encode [as 别名]
def _base16_encode(self, encode_string):
encoder = base64.b16encode(encode_string)
return encoder