本文整理匯總了Python中pyasn1.codec.der.encoder.encode方法的典型用法代碼示例。如果您正苦於以下問題:Python encoder.encode方法的具體用法?Python encoder.encode怎麽用?Python encoder.encode使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pyasn1.codec.der.encoder
的用法示例。
在下文中一共展示了encoder.encode方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: _derive_keys
# 需要導入模塊: from pyasn1.codec.der import encoder [as 別名]
# 或者: from pyasn1.codec.der.encoder import encode [as 別名]
def _derive_keys(self, password=None):
label = encode(self.encryption_oid) + encode(self.mac_oid)
context = self.nonce.asOctets()
backend = default_backend()
kdf = KBKDFHMAC(
algorithm=hashes.SHA256(),
mode=Mode.CounterMode,
length=48,
rlen=4,
llen=4,
location=CounterLocation.BeforeFixed,
label=label,
context=context,
fixed=None,
backend=backend
)
key = kdf.derive(password)
if self.DEBUG:
sys.stderr.write("Derived key: {0}\n".format(key))
self.encryption_key = key[0:16]
self.mac_key = key[16:]
示例2: check_dir
# 需要導入模塊: from pyasn1.codec.der import encoder [as 別名]
# 或者: from pyasn1.codec.der.encoder import encode [as 別名]
def check_dir(self, service, path, password = None):
path = string.replace(path,'/', '\\')
tid = self.tree_connect_andx('\\\\' + self.__remote_name + '\\' + service, password)
try:
smb = NewSMBPacket()
smb['Tid'] = tid
smb['Mid'] = 0
cmd = SMBCommand(SMB.SMB_COM_CHECK_DIRECTORY)
cmd['Parameters'] = ''
cmd['Data'] = SMBCheckDirectory_Data(flags = self.__flags2)
cmd['Data']['DirectoryName'] = path.encode('utf-16le') if self.__flags2 & SMB.FLAGS2_UNICODE else path
smb.addCommand(cmd)
self.sendSMB(smb)
while 1:
s = self.recvSMB()
if s.isValidAnswer(SMB.SMB_COM_CHECK_DIRECTORY):
return
finally:
self.disconnect_tree(tid)
示例3: rmdir
# 需要導入模塊: from pyasn1.codec.der import encoder [as 別名]
# 或者: from pyasn1.codec.der.encoder import encode [as 別名]
def rmdir(self, service, path, password = None):
path = string.replace(path,'/', '\\')
# Check that the directory exists
self.check_dir(service, path, password)
tid = self.tree_connect_andx('\\\\' + self.__remote_name + '\\' + service, password)
try:
path = path.encode('utf-16le') if self.__flags2 & SMB.FLAGS2_UNICODE else path
smb = NewSMBPacket()
smb['Tid'] = tid
createDir = SMBCommand(SMB.SMB_COM_DELETE_DIRECTORY)
createDir['Data'] = SMBDeleteDirectory_Data(flags=self.__flags2)
createDir['Data']['DirectoryName'] = path
smb.addCommand(createDir)
self.sendSMB(smb)
while 1:
s = self.recvSMB()
if s.isValidAnswer(SMB.SMB_COM_DELETE_DIRECTORY):
return
finally:
self.disconnect_tree(tid)
示例4: mkdir
# 需要導入模塊: from pyasn1.codec.der import encoder [as 別名]
# 或者: from pyasn1.codec.der.encoder import encode [as 別名]
def mkdir(self, service, path, password = None):
path = string.replace(path,'/', '\\')
tid = self.tree_connect_andx('\\\\' + self.__remote_name + '\\' + service, password)
try:
path = path.encode('utf-16le') if self.__flags2 & SMB.FLAGS2_UNICODE else path
smb = NewSMBPacket()
smb['Tid'] = tid
smb['Mid'] = 0
createDir = SMBCommand(SMB.SMB_COM_CREATE_DIRECTORY)
createDir['Data'] = SMBCreateDirectory_Data(flags=self.__flags2)
createDir['Data']['DirectoryName'] = path
smb.addCommand(createDir)
self.sendSMB(smb)
smb = self.recvSMB()
if smb.isValidAnswer(SMB.SMB_COM_CREATE_DIRECTORY):
return 1
return 0
finally:
self.disconnect_tree(tid)
示例5: rename
# 需要導入模塊: from pyasn1.codec.der import encoder [as 別名]
# 或者: from pyasn1.codec.der.encoder import encode [as 別名]
def rename(self, service, old_path, new_path, password = None):
old_path = string.replace(old_path,'/', '\\')
new_path = string.replace(new_path,'/', '\\')
tid = self.tree_connect_andx('\\\\' + self.__remote_name + '\\' + service, password)
try:
smb = NewSMBPacket()
smb['Tid'] = tid
smb['Mid'] = 0
renameCmd = SMBCommand(SMB.SMB_COM_RENAME)
renameCmd['Parameters'] = SMBRename_Parameters()
renameCmd['Parameters']['SearchAttributes'] = ATTR_SYSTEM | ATTR_HIDDEN | ATTR_DIRECTORY
renameCmd['Data'] = SMBRename_Data(flags = self.__flags2)
renameCmd['Data']['OldFileName'] = old_path.encode('utf-16le') if self.__flags2 & SMB.FLAGS2_UNICODE else old_path
renameCmd['Data']['NewFileName'] = new_path.encode('utf-16le') if self.__flags2 & SMB.FLAGS2_UNICODE else new_path
smb.addCommand(renameCmd)
self.sendSMB(smb)
smb = self.recvSMB()
if smb.isValidAnswer(SMB.SMB_COM_RENAME):
return 1
return 0
finally:
self.disconnect_tree(tid)
示例6: toTGT
# 需要導入模塊: from pyasn1.codec.der import encoder [as 別名]
# 或者: from pyasn1.codec.der.encoder import encode [as 別名]
def toTGT(self):
tgt_rep = AS_REP()
tgt_rep['pvno'] = 5
tgt_rep['msg-type'] = int(constants.ApplicationTagNumbers.AS_REP.value)
tgt_rep['crealm'] = self['server'].realm['data']
# Fake EncryptedData
tgt_rep['enc-part'] = None
tgt_rep['enc-part']['etype'] = 1
tgt_rep['enc-part']['cipher'] = ''
seq_set(tgt_rep, 'cname', self['client'].toPrincipal().components_to_asn1)
ticket = types.Ticket()
ticket.from_asn1(self.ticket['data'])
seq_set(tgt_rep,'ticket', ticket.to_asn1)
cipher = crypto._enctype_table[self['key']['keytype']]()
tgt = dict()
tgt['KDC_REP'] = encoder.encode(tgt_rep)
tgt['cipher'] = cipher
tgt['sessionKey'] = crypto.Key(cipher.enctype, str(self['key']['keyvalue']))
return tgt
示例7: _read_private_key
# 需要導入模塊: from pyasn1.codec.der import encoder [as 別名]
# 或者: from pyasn1.codec.der.encoder import encode [as 別名]
def _read_private_key(private_key_pem, passphrase=None):
"""Reads a private key PEM block and returns a RSAPrivatekey
:param private_key_pem: The private key PEM block
:param passphrase: Optional passphrase needed to decrypt the private key
:returns: a RSAPrivatekey object
"""
if passphrase and isinstance(passphrase, str):
passphrase = passphrase.encode("utf-8")
if isinstance(private_key_pem, str):
private_key_pem = private_key_pem.encode('utf-8')
try:
return serialization.load_pem_private_key(private_key_pem, passphrase,
backends.default_backend())
except Exception:
LOG.exception("Passphrase required.")
raise exceptions.NeedsPassphrase
示例8: get_intermediates_pems
# 需要導入模塊: from pyasn1.codec.der import encoder [as 別名]
# 或者: from pyasn1.codec.der.encoder import encode [as 別名]
def get_intermediates_pems(intermediates=None):
"""Split the input string into individual x509 text blocks
:param intermediates: PEM or PKCS7 encoded intermediate certificates
:returns: A list of strings where each string represents an
X509 pem block surrounded by BEGIN CERTIFICATE,
END CERTIFICATE block tags
"""
if isinstance(intermediates, str):
try:
intermediates = intermediates.encode("utf-8")
except UnicodeDecodeError:
LOG.debug("Couldn't encode intermediates string, it was probably "
"in binary DER format.")
if X509_BEG in intermediates:
for x509Pem in _split_x509s(intermediates):
yield _prepare_x509_cert(_get_x509_from_pem_bytes(x509Pem))
else:
for x509Pem in _parse_pkcs7_bundle(intermediates):
yield _prepare_x509_cert(_get_x509_from_der_bytes(x509Pem))
示例9: _process_pkcs7_substrate
# 需要導入模塊: from pyasn1.codec.der import encoder [as 別名]
# 或者: from pyasn1.codec.der.encoder import encode [as 別名]
def _process_pkcs7_substrate(substrate):
contentInfo, _ = der_decoder.decode(substrate,
asn1Spec=rfc2315.ContentInfo())
contentType = contentInfo.getComponentByName('contentType')
if contentType != rfc2315.signedData:
raise Exception
content, _ = der_decoder.decode(
contentInfo.getComponentByName('content'),
asn1Spec=rfc2315.SignedData())
for blob in content.getComponentByName('certificates'):
cert = x509.load_der_x509_certificate(der_encoder.encode(blob),
backends.default_backend())
print(cert.public_bytes(
encoding=serialization.Encoding.PEM).decode(
'unicode_escape'), end='')
# Main program code
示例10: toTGT
# 需要導入模塊: from pyasn1.codec.der import encoder [as 別名]
# 或者: from pyasn1.codec.der.encoder import encode [as 別名]
def toTGT(self):
tgt_rep = AS_REP()
tgt_rep['pvno'] = 5
tgt_rep['msg-type'] = int(constants.ApplicationTagNumbers.AS_REP.value)
tgt_rep['crealm'] = self['server'].realm['data']
# Fake EncryptedData
tgt_rep['enc-part'] = noValue
tgt_rep['enc-part']['etype'] = 1
tgt_rep['enc-part']['cipher'] = ''
seq_set(tgt_rep, 'cname', self['client'].toPrincipal().components_to_asn1)
ticket = types.Ticket()
ticket.from_asn1(self.ticket['data'])
seq_set(tgt_rep,'ticket', ticket.to_asn1)
cipher = crypto._enctype_table[self['key']['keytype']]()
tgt = dict()
tgt['KDC_REP'] = encoder.encode(tgt_rep)
tgt['cipher'] = cipher
tgt['sessionKey'] = crypto.Key(cipher.enctype, str(self['key']['keyvalue']))
return tgt
示例11: testDerCodec
# 需要導入模塊: from pyasn1.codec.der import encoder [as 別名]
# 或者: from pyasn1.codec.der.encoder import encode [as 別名]
def testDerCodec(self):
substrate = pem.readBase64fromText(self.rsa_cert_pem_text)
asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
self.assertFalse(rest)
self.assertTrue(asn1Object.prettyPrint())
self.assertEqual(substrate, der_encoder(asn1Object))
spki_a = asn1Object['tbsCertificate']['subjectPublicKeyInfo']['algorithm']
self.assertEqual(rfc3279.rsaEncryption, spki_a['algorithm'])
spki_pk = asn1Object['tbsCertificate']['subjectPublicKeyInfo']['subjectPublicKey'].asOctets()
pk, rest = der_decoder(spki_pk, asn1Spec=rfc3279.RSAPublicKey())
self.assertFalse(rest)
self.assertTrue(pk.prettyPrint())
self.assertEqual(spki_pk, der_encoder(pk))
self.assertEqual(65537, pk['publicExponent'])
self.assertEqual(rfc3279.sha1WithRSAEncryption,
asn1Object['tbsCertificate']['signature']['algorithm'])
self.assertEqual(rfc3279.sha1WithRSAEncryption,
asn1Object['signatureAlgorithm']['algorithm'])
示例12: testOpenTypes
# 需要導入模塊: from pyasn1.codec.der import encoder [as 別名]
# 或者: from pyasn1.codec.der.encoder import encode [as 別名]
def testOpenTypes(self):
substrate = pem.readBase64fromText(self.dsa_cert_pem_text)
asn1Object, rest = der_decoder(
substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
self.assertFalse(rest)
self.assertTrue(asn1Object.prettyPrint())
self.assertEqual(substrate, der_encoder(asn1Object))
spki_a = asn1Object['tbsCertificate']['subjectPublicKeyInfo']['algorithm']
self.assertEqual(rfc3279.id_dsa, spki_a['algorithm'])
q_value = 1260916123897116834511257683105158021801897369967
self.assertEqual(q_value, spki_a['parameters']['q'])
示例13: testDerCodec
# 需要導入模塊: from pyasn1.codec.der import encoder [as 別名]
# 或者: from pyasn1.codec.der.encoder import encode [as 別名]
def testDerCodec(self):
substrate = pem.readBase64fromText(self.extn_pem_text)
asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
self.assertFalse(rest)
self.assertTrue(asn1Object.prettyPrint())
self.assertEqual(substrate, der_encoder(asn1Object))
self.assertEqual(rfc8649.id_ce_hashOfRootKey, asn1Object['extnID'])
hashed_root_key, rest = der_decoder(
asn1Object['extnValue'], rfc8649.HashedRootKey())
self.assertFalse(rest)
self.assertTrue(hashed_root_key.prettyPrint())
self.assertEqual(asn1Object['extnValue'], der_encoder(hashed_root_key))
self.assertEqual(
rfc4055.id_sha512, hashed_root_key['hashAlg']['algorithm'])
示例14: testOpenTypes
# 需要導入模塊: from pyasn1.codec.der import encoder [as 別名]
# 或者: from pyasn1.codec.der.encoder import encode [as 別名]
def testOpenTypes(self):
substrate = pem.readBase64fromText(self.env_data_pem_text)
asn1Object, rest = der_decoder(
substrate, asn1Spec=self.asn1Spec, decodeOpenTypes=True)
self.assertFalse(rest)
self.assertTrue(asn1Object.prettyPrint())
self.assertEqual(substrate, der_encoder(asn1Object))
self.assertIn(asn1Object['contentType'], rfc5652.cmsContentTypesMap.keys())
kari_kea = asn1Object['content']['recipientInfos'][0]['kari']['keyEncryptionAlgorithm']
self.assertEqual(rfc2876.id_kEAKeyEncryptionAlgorithm, kari_kea['algorithm'])
self.assertEqual(rfc2876.id_fortezzaWrap80, kari_kea['parameters']['algorithm'])
cea = asn1Object['content']['encryptedContentInfo']['contentEncryptionAlgorithm']
self.assertEqual(rfc2876.id_fortezzaConfidentialityAlgorithm, cea['algorithm'])
iv = univ.OctetString(hexValue='424f4755535f4956')
self.assertEqual(iv, cea['parameters']['initialization-vector'])
示例15: testDerCodec
# 需要導入模塊: from pyasn1.codec.der import encoder [as 別名]
# 或者: from pyasn1.codec.der.encoder import encode [as 別名]
def testDerCodec(self):
substrate = pem.readBase64fromText(self.smime_capabilities_pem_text)
asn1Object, rest = der_decoder(substrate, asn1Spec=self.asn1Spec)
self.assertFalse(rest)
self.assertTrue(asn1Object.prettyPrint())
self.assertEqual(substrate, der_encoder(asn1Object))
found_wrap_alg = False
for cap in asn1Object:
if cap['capabilityID'] in rfc5751.smimeCapabilityMap.keys():
if cap['parameters'].hasValue():
param, rest = der_decoder(
cap['parameters'],
asn1Spec=rfc5751.smimeCapabilityMap[cap['capabilityID']])
self.assertFalse(rest)
self.assertTrue(param.prettyPrint())
self.assertEqual(cap['parameters'], der_encoder(param))
if cap['capabilityID'] == rfc2876.id_kEAKeyEncryptionAlgorithm:
self.assertEqual(rfc2876.id_fortezzaWrap80, param['algorithm'])
found_wrap_alg = True
self.assertTrue(found_wrap_alg)