Skip to content

Commit

Permalink
The secrets concept (#3565)
Browse files Browse the repository at this point in the history
* The `secrets` concept

* Edits and Link Additions

A few edits, and some additional links.

---------

Co-authored-by: BethanyG <[email protected]>
  • Loading branch information
colinleach and BethanyG authored Dec 27, 2023
1 parent 0b78195 commit 38caf4b
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 0 deletions.
5 changes: 5 additions & 0 deletions concepts/secrets/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"blurb": "The secrets module is a cryptographically-secure alternative to the random module, intended for security-critical uses.",
"authors": ["BethanyG", "colinleach"],
"contributors": []
}
51 changes: 51 additions & 0 deletions concepts/secrets/about.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# About

A previous concept discussed the [concept:python/random]() module, which produces [pseudo-random numbers][pseudo-random-numbers] and pseudo-random list orderings.

The [`secrets`][secrets] module overlaps with `random` in some of its functionality, but the two modules are designed with very different priorities.

- `random` is optimized for high performance in modelling and simulation, with "good enough" pseudo-random number generation.
- `secrets` is designed to be crytographically secure for applications such as password hashing, security token generation, and account authentication.


Further details on why the addition of the `secrets` module proved necessary are given in [PEP 506][PEP506].

The `secrets` is relatively small and straightforward, with methods for generating random integers, bits, bytes or tokens, or a random entry from a given sequence.

To use `scerets`, you mush first `import` it:


```python
>>> import secrets

#Returns n, where 0 <= n < 1000.
>>> secrets.randbelow(1000)
577

#32-bit integers.
>>> secrets.randbits(32)
3028709440

>>> bin(secrets.randbits(32))
'0b11111000101100101111110011110100'

#Pick at random from a sequence.
>>> secrets.choice(['my', 'secret', 'thing'])
'thing'

#Generate a token made up of random hexadecimal digits.
>>> secrets.token_hex()
'f683d093ea9aa1f2607497c837cf11d7afaefa903c5805f94b64f068e2b9e621'

#Generate a URL-safe token of random alphanumeric characters.
>>> secrets.token_urlsafe(16)
'gkSUKRdiPDHqmImPi2HMnw'
```


If you are writing security-sensitive applications, you will certainly want to read the [full documentation][secrets], which gives further advice and examples.


[PEP506]: https://peps.python.org/pep-0506/
[pseudo-random-numbers]: https://www.khanacademy.org/computing/computer-science/cryptography/crypt/v/random-vs-pseudorandom-number-generators
[secrets]: https://docs.python.org/3/library/secrets.html
17 changes: 17 additions & 0 deletions concepts/secrets/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Introduction

A previous concept discussed the [concept:python/random]() module, which produces [pseudo-random numbers][pseudo-random-numbers] and pseudo-random list orderings.

The [`secrets`][secrets] module overlaps with `random` in some of its functionality, but the two modules are designed with very different priorities.

- `random` is optimized for high performance in modelling and simulation, with "good enough" pseudo-random number generation.
- `secrets` is designed to be crytographically secure for applications such as password hashing, security token generation, and account authentication.


Further details on why the addition of the `secrets` module proved necessary are given in [PEP 506][PEP506].

If you are writing security-sensitive applications, you will certainly want to read the [full documentation][secrets], which gives further advice and examples.

[PEP506]: https://peps.python.org/pep-0506/[secrets]: https://docs.python.org/3/library/secrets.html
[pseudo-random-numbers]: https://www.khanacademy.org/computing/computer-science/cryptography/crypt/v/random-vs-pseudorandom-number-generators
[secrets]: https://docs.python.org/3/library/secrets.html
18 changes: 18 additions & 0 deletions concepts/secrets/links.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"url": "https://docs.python.org/3/library/secrets.html/",
"description": "The secrets module."
},
{
"url": "https://peps.python.org/pep-0506/",
"description": "PEP 506, giving reasons why the secrets module is necessary."
},
{
"url": "https://en.wikipedia.org/wiki/Pseudorandomness",
"description": "Wikipedia: Pseudorandomness."
},
{
"url": "https://www.khanacademy.org/computing/computer-science/cryptography/crypt/v/random-vs-pseudorandom-number-generators",
"description": "Khan Academy: Pseudorandom Number Generators."
}
]
5 changes: 5 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2582,6 +2582,11 @@
"uuid": "000e7768-38b9-4904-9ae2-9a4e448f366c",
"slug": "fractions",
"name": "Fractions"
},
{
"uuid": "e1496136-8d58-4409-9a41-4b6ee4721c6b",
"slug": "secrets",
"name": "Secrets"
}
],
"key_features": [
Expand Down

0 comments on commit 38caf4b

Please sign in to comment.