]>
gitweb.pimeys.fr Git - NK2015_Client_Python_Alpha.git/blob - rsa_source/rsa/common.py
1 # -*- coding: utf-8 -*-
3 # Copyright 2011 Sybren A. Stüvel <sybren@stuvel.eu>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 '''Common functionality shared by several modules.'''
23 '''Returns the number of bits required to hold a specific long number.
32 >>> bit_size(1 << 1024)
34 >>> bit_size((1 << 1024) + 1)
36 >>> bit_size((1 << 1024) - 1)
42 raise ValueError('Only nonnegative numbers possible: %s' % number
)
47 # This works, even with very large numbers. When using math.log(number, 2),
48 # you'll get rounding errors and it'll fail.
57 def byte_size(number
):
58 """Returns the number of bytes required to hold a specific long number.
60 The number of bytes is rounded up.
62 >>> byte_size(1 << 1023)
64 >>> byte_size((1 << 1024) - 1)
66 >>> byte_size(1 << 1024)
70 return int(math
.ceil(bit_size(number
) / 8.0))
72 if __name__
== '__main__':