| 1 |
""" |
| 2 |
Copyright (c) 2014 Congressus, The Netherlands |
| 3 |
Copyright (c) 2017-2023 Raphael Michel and contributors |
| 4 |
|
| 5 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
of this software and associated documentation files (the "Software"), to deal |
| 7 |
in the Software without restriction, including without limitation the rights |
| 8 |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 |
copies of the Software, and to permit persons to whom the Software is furnished |
| 10 |
to do so, subject to the following conditions: |
| 11 |
|
| 12 |
The above copyright notice and this permission notice shall be included in all |
| 13 |
copies or substantial portions of the Software. |
| 14 |
|
| 15 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 19 |
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 20 |
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 21 |
""" |
| 22 |
import datetime |
| 23 |
import hashlib |
| 24 |
import random |
| 25 |
import re |
| 26 |
import time |
| 27 |
|
| 28 |
try: |
| 29 |
random = random.SystemRandom() |
| 30 |
using_sysrandom = True |
| 31 |
except NotImplementedError: |
| 32 |
import warnings |
| 33 |
warnings.warn('A secure pseudo-random number generator is not available ' |
| 34 |
'on your system. Falling back to Mersenne Twister.') |
| 35 |
using_sysrandom = False |
| 36 |
|
| 37 |
|
| 38 |
def get_rand_string(length=12, allowed_chars='0123456789abcdef'): |
| 39 |
""" |
| 40 |
Returns a securely generated random string. Taken from the Django project |
| 41 |
|
| 42 |
The default length of 12 with the a-z, A-Z, 0-9 character set returns |
| 43 |
a 71-bit value. log_2((26+26+10)^12) =~ 71 bits |
| 44 |
""" |
| 45 |
if not using_sysrandom: |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
random.seed( |
| 53 |
hashlib.sha256( |
| 54 |
("%s%s" % ( |
| 55 |
random.getstate(), |
| 56 |
time.time())).encode('utf-8') |
| 57 |
).digest()) |
| 58 |
return ''.join([random.choice(allowed_chars) for i in range(length)]) |
| 59 |
|
| 60 |
|
| 61 |
def make_msg_id(): |
| 62 |
""" |
| 63 |
Create a semi random message id, by using 12 char random hex string and |
| 64 |
a timestamp. |
| 65 |
@return: string consisting of timestamp, -, random value |
| 66 |
""" |
| 67 |
random_string = get_rand_string(12) |
| 68 |
timestamp = datetime.datetime.now().strftime("%Y%m%d%I%M%S") |
| 69 |
msg_id = timestamp + "-" + random_string |
| 70 |
return msg_id |
| 71 |
|
| 72 |
|
| 73 |
def make_id(name): |
| 74 |
""" |
| 75 |
Create a random id combined with the creditor name. |
| 76 |
@return string consisting of name (truncated at 22 chars), -, |
| 77 |
12 char rand hex string. |
| 78 |
""" |
| 79 |
name = re.sub(r'[^a-zA-Z0-9]', '', name) |
| 80 |
r = get_rand_string(12) |
| 81 |
if len(name) > 22: |
| 82 |
name = name[:22] |
| 83 |
return name + "-" + r |
| 84 |
|
| 85 |
|
| 86 |
def int_to_decimal_str(integer): |
| 87 |
""" |
| 88 |
Helper to convert integers (representing cents) into decimal currency |
| 89 |
string. WARNING: DO NOT TRY TO DO THIS BY DIVISION, FLOATING POINT |
| 90 |
ERRORS ARE NO FUN IN FINANCIAL SYSTEMS. |
| 91 |
@param integer The amount in cents |
| 92 |
@return string The amount in currency with full stop decimal separator |
| 93 |
""" |
| 94 |
int_string = str(integer) |
| 95 |
if len(int_string) <= 2: |
| 96 |
return "0." + int_string.zfill(2) |
| 97 |
else: |
| 98 |
return int_string[:-2] + "." + int_string[-2:] |
| 99 |
|
| 100 |
|
| 101 |
def decimal_str_to_int(decimal_string): |
| 102 |
""" |
| 103 |
Helper to decimal currency string into integers (cents). |
| 104 |
WARNING: DO NOT TRY TO DO THIS BY CONVERSION AND MULTIPLICATION, |
| 105 |
FLOATING POINT ERRORS ARE NO FUN IN FINANCIAL SYSTEMS. |
| 106 |
@param string The amount in currency with full stop decimal separator |
| 107 |
@return integer The amount in cents |
| 108 |
""" |
| 109 |
int_string = decimal_string.replace('.', '') |
| 110 |
int_string = int_string.lstrip('0') |
| 111 |
return int(int_string) |
| 112 |
|
| 113 |
|
| 114 |
ADDRESS_MAPPING = ( |
| 115 |
("address_type", "AdrTp"), |
| 116 |
("department", "Dept"), |
| 117 |
("subdepartment", "SubDept"), |
| 118 |
("street_name", "StrtNm"), |
| 119 |
("building_number", "BldgNb"), |
| 120 |
("postcode", "PstCd"), |
| 121 |
("town", "TwnNm"), |
| 122 |
("country_subdivision", "CtrySubDvsn"), |
| 123 |
("country", "Ctry"), |
| 124 |
) |