| Trees | Indices | Help |
|
|---|
|
|
1 # common crypto functions (mostly specific to XEP-0116, but useful elsewhere) 2 # -*- coding:utf-8 -*- 3 ## src/common/crypto.py 4 ## 5 ## Copyright (C) 2007 Brendan Taylor <whateley AT gmail.com> 6 ## 7 ## This file is part of Gajim. 8 ## 9 ## Gajim is free software; you can redistribute it and/or modify 10 ## it under the terms of the GNU General Public License as published 11 ## by the Free Software Foundation; version 3 only. 12 ## 13 ## Gajim is distributed in the hope that it will be useful, 14 ## but WITHOUT ANY WARRANTY; without even the implied warranty of 15 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 ## GNU General Public License for more details. 17 ## 18 ## You should have received a copy of the GNU General Public License 19 ## along with Gajim. If not, see <http://www.gnu.org/licenses/>. 20 ## 21 22 import os 23 import math 24 25 from hashlib import sha256 as SHA256 26 27 # convert a large integer to a big-endian bitstring 33 34 # convert a large integer to a big-endian bitstring, padded with \x00s to 35 # a multiple of 16 bytes 38 39 # pad 'string' to a multiple of 'multiple_of' with 'char'. 40 # pad on the left if 'left', otherwise pad on the right.42 mod = len(string) % multiple_of 43 if mod == 0: 44 return string 45 else: 46 padding = (multiple_of - mod) * char 47 48 if left: 49 return padding + string 50 else: 51 return string + padding52 53 # convert a big-endian bitstring to an integer 59 64 65 base28_chr = "acdefghikmopqruvwxy123456789" 6668 sha = sha256(m_a + form_b + 'Short Authentication String') 69 lsb24 = decode_mpi(sha[-3:]) 70 return base28(lsb24)71 77 8082 return random_bytes(8)83 84 # generate a random number between 'bottom' and 'top'86 # minimum number of bytes needed to represent that range 87 bytes = int(math.ceil(math.log(top - bottom, 256))) 88 89 # in retrospect, this is horribly inadequate. 90 return (decode_mpi(random_bytes(bytes)) % (top - bottom)) + bottom91 92 # a faster version of (base ** exp) % mod 93 # taken from <http://lists.danga.com/pipermail/yadis/2005-September/001445.html>95 square = base % mod 96 result = 1 97 98 while exp > 0: 99 if exp & 1: # exponent is odd 100 result = (result * square) % mod 101 102 square = (square * square) % mod 103 exp /= 2 104 105 return result106
| Trees | Indices | Help |
|
|---|
| Generated by Epydoc 3.0.1 on Thu Aug 12 02:07:55 2010 | http://epydoc.sourceforge.net |