Return a securely generated random string. The bit length of the returned value can be calculated with the formula: log_2(len(allowed_chars)^length) For example, with default `allowed_chars` (26+26+10), this gives: * length: 12, bit length =~ 71 bits * length: 22,
(length, allowed_chars=RANDOM_STRING_CHARS)
| 64 | |
| 65 | |
| 66 | def get_random_string(length, allowed_chars=RANDOM_STRING_CHARS): |
| 67 | """ |
| 68 | Return a securely generated random string. |
| 69 | |
| 70 | The bit length of the returned value can be calculated with the formula: |
| 71 | log_2(len(allowed_chars)^length) |
| 72 | |
| 73 | For example, with default `allowed_chars` (26+26+10), this gives: |
| 74 | * length: 12, bit length =~ 71 bits |
| 75 | * length: 22, bit length =~ 131 bits |
| 76 | """ |
| 77 | return "".join(secrets.choice(allowed_chars) for i in range(length)) |
| 78 | |
| 79 | |
| 80 | def constant_time_compare(val1, val2): |