diff options
author | Michael Manganiello <adamantike@users.noreply.github.com> | 2017-04-18 03:59:58 -0300 |
---|---|---|
committer | Sybren A. Stüvel <sybren@stuvel.eu> | 2017-04-18 08:59:58 +0200 |
commit | 000e84a97a8ab25a5d9b4185a2e02efc033f7e8a (patch) | |
tree | 99501a6b7f2785a4002d5f0bf4a5e815c2fd881a /rsa/common.py | |
parent | 7ebae9f96ec6aba420bfdf7f6467be0e6b30275c (diff) | |
download | rsa-git-000e84a97a8ab25a5d9b4185a2e02efc033f7e8a.tar.gz |
Ceiling division implementation (#88)
Created as a new function as it will be needed by the new PKCS#1 2.0 implementation. Specifically, for the MGF1 function used in the OAEP encoding/decoding.
This allows us not to have `math` dependencies
Diffstat (limited to 'rsa/common.py')
-rw-r--r-- | rsa/common.py | 28 |
1 files changed, 25 insertions, 3 deletions
diff --git a/rsa/common.py b/rsa/common.py index 7c8a082..f7aa2d1 100644 --- a/rsa/common.py +++ b/rsa/common.py @@ -76,11 +76,33 @@ def byte_size(number): :returns: The number of bytes required to hold a specific long number. """ - quanta, mod = divmod(bit_size(number), 8) - if mod or number == 0: + if number == 0: + return 1 + return ceil_div(bit_size(number), 8) + + +def ceil_div(num, div): + """ + Returns the ceiling function of a division between `num` and `div`. + + Usage:: + + >>> ceil_div(100, 7) + 15 + >>> ceil_div(100, 10) + 10 + >>> ceil_div(1, 4) + 1 + + :param num: Division's numerator, a number + :param div: Division's divisor, a number + + :return: Rounded up result of the division between the parameters. + """ + quanta, mod = divmod(num, div) + if mod: quanta += 1 return quanta - # return int(math.ceil(bit_size(number) / 8.0)) def extended_gcd(a, b): |