From 05de66db6dde903550862dc08a5409ac102fa487 Mon Sep 17 00:00:00 2001 From: Robey Pointer Date: Tue, 1 Aug 2006 11:23:57 -0700 Subject: [PATCH] [project @ robey@lag.net-20060801182357-e030af9570314145] use hexlify instead of home-grown hexify --- paramiko/client.py | 14 +++++++------- paramiko/pkey.py | 5 +++-- paramiko/sftp_client.py | 4 +++- paramiko/sftp_file.py | 14 ++++++++------ paramiko/util.py | 7 +++---- 5 files changed, 24 insertions(+), 20 deletions(-) diff --git a/paramiko/client.py b/paramiko/client.py index b97445b..a0a316f 100644 --- a/paramiko/client.py +++ b/paramiko/client.py @@ -20,6 +20,7 @@ L{SSHClient}. """ +from binascii import hexlify import getpass import os @@ -30,7 +31,6 @@ from paramiko.hostkeys import HostKeys from paramiko.rsakey import RSAKey from paramiko.ssh_exception import SSHException, BadHostKeyException from paramiko.transport import Transport -from paramiko.util import hexify class MissingHostKeyPolicy (object): @@ -65,7 +65,7 @@ class AutoAddPolicy (MissingHostKeyPolicy): if client._host_keys_filename is not None: client.save_host_keys(client._host_keys_filename) 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): @@ -76,7 +76,7 @@ class RejectPolicy (MissingHostKeyPolicy): def missing_host_key(self, client, hostname, key): 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) @@ -350,7 +350,7 @@ class SSHClient (object): if pkey is not None: 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) return except SSHException, e: @@ -360,7 +360,7 @@ class SSHClient (object): for pkey_class in (RSAKey, DSSKey): try: 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) return except SSHException, e: @@ -368,7 +368,7 @@ class SSHClient (object): for key in Agent().get_keys(): 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) return except SSHException, e: @@ -379,7 +379,7 @@ class SSHClient (object): filename = os.path.expanduser('~/.ssh/' + filename) try: 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) return except SSHException, e: diff --git a/paramiko/pkey.py b/paramiko/pkey.py index e1aef88..47f2378 100644 --- a/paramiko/pkey.py +++ b/paramiko/pkey.py @@ -21,6 +21,7 @@ Common API for all public keys. """ import base64 +from binascii import hexlify, unhexlify import os from Crypto.Hash import MD5 @@ -322,7 +323,7 @@ class PKey (object): cipher = self._CIPHER_TABLE[encryption_type]['cipher'] keysize = self._CIPHER_TABLE[encryption_type]['keysize'] mode = self._CIPHER_TABLE[encryption_type]['mode'] - salt = util.unhexify(saltstr) + salt = unhexlify(saltstr) key = util.generate_key_bytes(MD5, salt, password, keysize) return cipher.new(key, mode, salt).decrypt(data) @@ -368,7 +369,7 @@ class PKey (object): data += '\0' * n data = cipher.new(key, mode, salt).encrypt(data) 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') s = base64.encodestring(data) # re-wrap to 64-char lines diff --git a/paramiko/sftp_client.py b/paramiko/sftp_client.py index f8f29c5..e4f0d7c 100644 --- a/paramiko/sftp_client.py +++ b/paramiko/sftp_client.py @@ -20,11 +20,13 @@ Client-mode SFTP support. """ +from binascii import hexlify import errno import os import threading import time import weakref + from paramiko.sftp import * from paramiko.sftp_attr import SFTPAttributes from paramiko.ssh_exception import SSHException @@ -220,7 +222,7 @@ class SFTPClient (BaseSFTP): if t != CMD_HANDLE: raise SFTPError('Expected handle') 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) # python has migrated toward file() instead of open(). diff --git a/paramiko/sftp_file.py b/paramiko/sftp_file.py index 9dafbaf..5050a21 100644 --- a/paramiko/sftp_file.py +++ b/paramiko/sftp_file.py @@ -20,9 +20,11 @@ L{SFTPFile} """ +from binascii import hexlify import socket import threading import time + from paramiko.common import * from paramiko.sftp import * from paramiko.file import BufferedFile @@ -66,7 +68,7 @@ class SFTPFile (BufferedFile): # __del__.) if self._closed: return - self.sftp._log(DEBUG, 'close(%s)' % util.hexify(self.handle)) + self.sftp._log(DEBUG, 'close(%s)' % hexlify(self.handle)) if self.pipelined: self.sftp._finish_responses(self) BufferedFile.close(self) @@ -232,7 +234,7 @@ class SFTPFile (BufferedFile): @param mode: new permissions @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.st_mode = mode self.sftp._request(CMD_FSETSTAT, self.handle, attr) @@ -249,7 +251,7 @@ class SFTPFile (BufferedFile): @param gid: new group id @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.st_uid, attr.st_gid = uid, gid self.sftp._request(CMD_FSETSTAT, self.handle, attr) @@ -269,7 +271,7 @@ class SFTPFile (BufferedFile): """ if times is None: 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.st_atime, attr.st_mtime = times self.sftp._request(CMD_FSETSTAT, self.handle, attr) @@ -283,7 +285,7 @@ class SFTPFile (BufferedFile): @param size: the new size of the file @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.st_size = size self.sftp._request(CMD_FSETSTAT, self.handle, attr) @@ -402,7 +404,7 @@ class SFTPFile (BufferedFile): @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 = [] for offset, size in chunks: diff --git a/paramiko/util.py b/paramiko/util.py index a8fcf6e..c8c752d 100644 --- a/paramiko/util.py +++ b/paramiko/util.py @@ -22,6 +22,7 @@ Useful functions used by the rest of paramiko. from __future__ import generators +from binascii import hexlify, unhexlify import sys import struct import traceback @@ -115,12 +116,10 @@ def format_binary_line(data): return '%-50s %s' % (left, right) def hexify(s): - "turn a string into a hex sequence" - return ''.join(['%02X' % ord(c) for c in s]) + return hexlify(s).upper() def unhexify(s): - "turn a hex sequence back into a string" - return ''.join([chr(int(s[i:i+2], 16)) for i in range(0, len(s), 2)]) + return unhexlify(s) def safe_string(s): out = ''