use hexlify instead of home-grown hexify
This commit is contained in:
Robey Pointer 2006-08-01 11:23:57 -07:00
parent 074dc36e6b
commit 05de66db6d
5 changed files with 24 additions and 20 deletions

View File

@ -20,6 +20,7 @@
L{SSHClient}. L{SSHClient}.
""" """
from binascii import hexlify
import getpass import getpass
import os import os
@ -30,7 +31,6 @@ from paramiko.hostkeys import HostKeys
from paramiko.rsakey import RSAKey from paramiko.rsakey import RSAKey
from paramiko.ssh_exception import SSHException, BadHostKeyException from paramiko.ssh_exception import SSHException, BadHostKeyException
from paramiko.transport import Transport from paramiko.transport import Transport
from paramiko.util import hexify
class MissingHostKeyPolicy (object): class MissingHostKeyPolicy (object):
@ -65,7 +65,7 @@ class AutoAddPolicy (MissingHostKeyPolicy):
if client._host_keys_filename is not None: if client._host_keys_filename is not None:
client.save_host_keys(client._host_keys_filename) client.save_host_keys(client._host_keys_filename)
client._log(DEBUG, 'Adding %s host key for %s: %s' % client._log(DEBUG, 'Adding %s host key for %s: %s' %
(key.get_name(), hostname, hexify(key.get_fingerprint()))) (key.get_name(), hostname, hexlify(key.get_fingerprint())))
class RejectPolicy (MissingHostKeyPolicy): class RejectPolicy (MissingHostKeyPolicy):
@ -76,7 +76,7 @@ class RejectPolicy (MissingHostKeyPolicy):
def missing_host_key(self, client, hostname, key): def missing_host_key(self, client, hostname, key):
client._log(DEBUG, 'Rejecting %s host key for %s: %s' % client._log(DEBUG, 'Rejecting %s host key for %s: %s' %
(key.get_name(), hostname, hexify(key.get_fingerprint()))) (key.get_name(), hostname, hexlify(key.get_fingerprint())))
raise SSHException('Unknown server %s' % hostname) raise SSHException('Unknown server %s' % hostname)
@ -350,7 +350,7 @@ class SSHClient (object):
if pkey is not None: if pkey is not None:
try: try:
self._log(DEBUG, 'Trying SSH key %s' % hexify(pkey.get_fingerprint())) self._log(DEBUG, 'Trying SSH key %s' % hexlify(pkey.get_fingerprint()))
self._transport.auth_publickey(username, pkey) self._transport.auth_publickey(username, pkey)
return return
except SSHException, e: except SSHException, e:
@ -360,7 +360,7 @@ class SSHClient (object):
for pkey_class in (RSAKey, DSSKey): for pkey_class in (RSAKey, DSSKey):
try: try:
key = pkey_class.from_private_key_file(key_filename, password) key = pkey_class.from_private_key_file(key_filename, password)
self._log(DEBUG, 'Trying key %s from %s' % (hexify(key.get_fingerprint()), key_filename)) self._log(DEBUG, 'Trying key %s from %s' % (hexlify(key.get_fingerprint()), key_filename))
self._transport.auth_publickey(username, key) self._transport.auth_publickey(username, key)
return return
except SSHException, e: except SSHException, e:
@ -368,7 +368,7 @@ class SSHClient (object):
for key in Agent().get_keys(): for key in Agent().get_keys():
try: try:
self._log(DEBUG, 'Trying SSH agent key %s' % hexify(key.get_fingerprint())) self._log(DEBUG, 'Trying SSH agent key %s' % hexlify(key.get_fingerprint()))
self._transport.auth_publickey(username, key) self._transport.auth_publickey(username, key)
return return
except SSHException, e: except SSHException, e:
@ -379,7 +379,7 @@ class SSHClient (object):
filename = os.path.expanduser('~/.ssh/' + filename) filename = os.path.expanduser('~/.ssh/' + filename)
try: try:
key = pkey_class.from_private_key_file(filename, password) key = pkey_class.from_private_key_file(filename, password)
self._log(DEBUG, 'Trying discovered key %s in %s' % (hexify(key.get_fingerprint()), filename)) self._log(DEBUG, 'Trying discovered key %s in %s' % (hexlify(key.get_fingerprint()), filename))
self._transport.auth_publickey(username, key) self._transport.auth_publickey(username, key)
return return
except SSHException, e: except SSHException, e:

View File

@ -21,6 +21,7 @@ Common API for all public keys.
""" """
import base64 import base64
from binascii import hexlify, unhexlify
import os import os
from Crypto.Hash import MD5 from Crypto.Hash import MD5
@ -322,7 +323,7 @@ class PKey (object):
cipher = self._CIPHER_TABLE[encryption_type]['cipher'] cipher = self._CIPHER_TABLE[encryption_type]['cipher']
keysize = self._CIPHER_TABLE[encryption_type]['keysize'] keysize = self._CIPHER_TABLE[encryption_type]['keysize']
mode = self._CIPHER_TABLE[encryption_type]['mode'] mode = self._CIPHER_TABLE[encryption_type]['mode']
salt = util.unhexify(saltstr) salt = unhexlify(saltstr)
key = util.generate_key_bytes(MD5, salt, password, keysize) key = util.generate_key_bytes(MD5, salt, password, keysize)
return cipher.new(key, mode, salt).decrypt(data) return cipher.new(key, mode, salt).decrypt(data)
@ -368,7 +369,7 @@ class PKey (object):
data += '\0' * n data += '\0' * n
data = cipher.new(key, mode, salt).encrypt(data) data = cipher.new(key, mode, salt).encrypt(data)
f.write('Proc-Type: 4,ENCRYPTED\n') f.write('Proc-Type: 4,ENCRYPTED\n')
f.write('DEK-Info: %s,%s\n' % (cipher_name, util.hexify(salt))) f.write('DEK-Info: %s,%s\n' % (cipher_name, hexlify(salt).upper()))
f.write('\n') f.write('\n')
s = base64.encodestring(data) s = base64.encodestring(data)
# re-wrap to 64-char lines # re-wrap to 64-char lines

View File

@ -20,11 +20,13 @@
Client-mode SFTP support. Client-mode SFTP support.
""" """
from binascii import hexlify
import errno import errno
import os import os
import threading import threading
import time import time
import weakref import weakref
from paramiko.sftp import * from paramiko.sftp import *
from paramiko.sftp_attr import SFTPAttributes from paramiko.sftp_attr import SFTPAttributes
from paramiko.ssh_exception import SSHException from paramiko.ssh_exception import SSHException
@ -220,7 +222,7 @@ class SFTPClient (BaseSFTP):
if t != CMD_HANDLE: if t != CMD_HANDLE:
raise SFTPError('Expected handle') raise SFTPError('Expected handle')
handle = msg.get_string() handle = msg.get_string()
self._log(DEBUG, 'open(%r, %r) -> %s' % (filename, mode, util.hexify(handle))) self._log(DEBUG, 'open(%r, %r) -> %s' % (filename, mode, hexlify(handle)))
return SFTPFile(self, handle, mode, bufsize) return SFTPFile(self, handle, mode, bufsize)
# python has migrated toward file() instead of open(). # python has migrated toward file() instead of open().

View File

@ -20,9 +20,11 @@
L{SFTPFile} L{SFTPFile}
""" """
from binascii import hexlify
import socket import socket
import threading import threading
import time import time
from paramiko.common import * from paramiko.common import *
from paramiko.sftp import * from paramiko.sftp import *
from paramiko.file import BufferedFile from paramiko.file import BufferedFile
@ -66,7 +68,7 @@ class SFTPFile (BufferedFile):
# __del__.) # __del__.)
if self._closed: if self._closed:
return return
self.sftp._log(DEBUG, 'close(%s)' % util.hexify(self.handle)) self.sftp._log(DEBUG, 'close(%s)' % hexlify(self.handle))
if self.pipelined: if self.pipelined:
self.sftp._finish_responses(self) self.sftp._finish_responses(self)
BufferedFile.close(self) BufferedFile.close(self)
@ -232,7 +234,7 @@ class SFTPFile (BufferedFile):
@param mode: new permissions @param mode: new permissions
@type mode: int @type mode: int
""" """
self.sftp._log(DEBUG, 'chmod(%s, %r)' % (util.hexify(self.handle), mode)) self.sftp._log(DEBUG, 'chmod(%s, %r)' % (hexlify(self.handle), mode))
attr = SFTPAttributes() attr = SFTPAttributes()
attr.st_mode = mode attr.st_mode = mode
self.sftp._request(CMD_FSETSTAT, self.handle, attr) self.sftp._request(CMD_FSETSTAT, self.handle, attr)
@ -249,7 +251,7 @@ class SFTPFile (BufferedFile):
@param gid: new group id @param gid: new group id
@type gid: int @type gid: int
""" """
self.sftp._log(DEBUG, 'chown(%s, %r, %r)' % (util.hexify(self.handle), uid, gid)) self.sftp._log(DEBUG, 'chown(%s, %r, %r)' % (hexlify(self.handle), uid, gid))
attr = SFTPAttributes() attr = SFTPAttributes()
attr.st_uid, attr.st_gid = uid, gid attr.st_uid, attr.st_gid = uid, gid
self.sftp._request(CMD_FSETSTAT, self.handle, attr) self.sftp._request(CMD_FSETSTAT, self.handle, attr)
@ -269,7 +271,7 @@ class SFTPFile (BufferedFile):
""" """
if times is None: if times is None:
times = (time.time(), time.time()) times = (time.time(), time.time())
self.sftp._log(DEBUG, 'utime(%s, %r)' % (util.hexify(self.handle), times)) self.sftp._log(DEBUG, 'utime(%s, %r)' % (hexlify(self.handle), times))
attr = SFTPAttributes() attr = SFTPAttributes()
attr.st_atime, attr.st_mtime = times attr.st_atime, attr.st_mtime = times
self.sftp._request(CMD_FSETSTAT, self.handle, attr) self.sftp._request(CMD_FSETSTAT, self.handle, attr)
@ -283,7 +285,7 @@ class SFTPFile (BufferedFile):
@param size: the new size of the file @param size: the new size of the file
@type size: int or long @type size: int or long
""" """
self.sftp._log(DEBUG, 'truncate(%s, %r)' % (util.hexify(self.handle), size)) self.sftp._log(DEBUG, 'truncate(%s, %r)' % (hexlify(self.handle), size))
attr = SFTPAttributes() attr = SFTPAttributes()
attr.st_size = size attr.st_size = size
self.sftp._request(CMD_FSETSTAT, self.handle, attr) self.sftp._request(CMD_FSETSTAT, self.handle, attr)
@ -402,7 +404,7 @@ class SFTPFile (BufferedFile):
@since: 1.5.4 @since: 1.5.4
""" """
self.sftp._log(DEBUG, 'readv(%s, %r)' % (util.hexify(self.handle), chunks)) self.sftp._log(DEBUG, 'readv(%s, %r)' % (hexlify(self.handle), chunks))
read_chunks = [] read_chunks = []
for offset, size in chunks: for offset, size in chunks:

View File

@ -22,6 +22,7 @@ Useful functions used by the rest of paramiko.
from __future__ import generators from __future__ import generators
from binascii import hexlify, unhexlify
import sys import sys
import struct import struct
import traceback import traceback
@ -115,12 +116,10 @@ def format_binary_line(data):
return '%-50s %s' % (left, right) return '%-50s %s' % (left, right)
def hexify(s): def hexify(s):
"turn a string into a hex sequence" return hexlify(s).upper()
return ''.join(['%02X' % ord(c) for c in s])
def unhexify(s): def unhexify(s):
"turn a hex sequence back into a string" return unhexlify(s)
return ''.join([chr(int(s[i:i+2], 16)) for i in range(0, len(s), 2)])
def safe_string(s): def safe_string(s):
out = '' out = ''