securitylib


Namesecuritylib JSON
Version 1.0.9 PyPI version JSON
download
home_pagehttp://oss.sapo.pt/securitylib-python/
SummarySAPO Security Lib - Python
upload_time2024-12-11 09:34:15
maintainerNone
docs_urlNone
authorJose Nuno Pires
requires_python>=3.4
licenseMIT License
keywords security crypto securitylib
VCS
bugtrack_url
requirements pycryptodome pytest pytest-sugar
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # SAPO Security Lib - Python

The SAPO Security Lib is a library whose purpose is to provide functions/classes
that solve common security related problems, while being easy to use even by
those who are not security experts. This repository contains the Python version
of this library.

Our design principles:

- **Security** — This is an obvious one, but it is important to explain how it
  is enforced.
    - No security primitives were invented, all security sensitive code is based
      on modern security best-practices (e.g. we use PBKDF2 to derive keys from
      passwords, we didn't reinvent the wheel).
    - Very high (near 100%) testing code coverage.
    - Manual code review by security professionals.
- **Security by default** — Using the library with the default parameters should
  provide enough security for most cases (maybe not military grade top security,
  but enough for an application like Gmail, for example). Flexibility was even
  traded in some places for increased security, for example by making it hard
  (i.e., impossible without messing with the lib code) for someone to use a weak
  algorithm instead of the default one.
- **Simple API** — Unfortunately, the acronyms AES, PBKDF2, HMAC, etc. are
  cryptic for many developers, and many others know them but might have
  difficulty knowing when and how to use them. As such, we decided to hide the
  implementation details in the API function names, resulting in names such as
  `generate_encryption_key`, `encrypt`, `prepare_password_for_storage`, etc.
  which most developers are able to understand even if they are not security
  experts.



There are currently 4 modules in this library:

- **crypto** — Cryptographic functions library.
- **advanced_crypto** — Advanced cryptographic functions library.
- **random** — Secure generation of random numbers and strings.
- **passwords** — Creation and validation of user passwords.

Some examples of use cases for each of these modules are given below.

For the full documentation of the library, go [here](http://oss.sapo.pt/securitylib-python/).


## Discussion

Please file any bugs you find in our [issue tracker](https://github.com/sapo/securitylib-python).

## Testing
- tox run

## Building wheel
- python setup.py bdist

_You should change version in setup.py when upgrading_

## Installation

Upgraded to support from Python 3.5 to 3.12.
There are several ways to install SAPO Security Lib.

### Via PyPI

Just run:
`pip install securitylib`


### Via a tarball release

1. Dowload the [tarball](https://github.com/sapo/securitylib-python/archive/1.0.0.tar.gz)
2. Unpack the tarball
3. `python setup.py install`


## Examples:

### Crypto

Generating a key for encryption:

```python
import securitylib

encryption_key = securitylib.crypto.generate_encryption_key()

print(encryption_key)
```

Generating a key for encryption based on a user's password:

```python
import securitylib

password = 'this_is_the_users_password'
salt = securitylib.random.get_random_token()
encryption_key = securitylib.crypto.generate_encryption_key_from_password(password, salt)

print(encryption_key)
```

Encrypting and decrypting data:

```python
import securitylib

data = 'this_is_the_data_we_want_to_encrypt'
encryption_key = securitylib.crypto.generate_encryption_key()
authenticator_key = securitylib.crypto.generate_authenticator_key()
encrypted_data = securitylib.crypto.encrypt(data, encryption_key, authenticator_key)
decrypted_data = securitylib.crypto.decrypt(encrypted_data, encryption_key, authenticator_key)
assert(decrypted_data == data)
```

### Advanced Crypto

Using a stream cipher to encrypt or decrypt a stream:

```python
import securitylib

data_chunks = ['this_is_', 'the_data', '_we', '_want_to_', 'encrypt']

encryption_key = securitylib.crypto.generate_encryption_key()

# Data can be encrypted chunk by chunk
stream_cipher = securitylib.advanced_crypto.StreamCipher(encryption_key)
encrypted_data = ''.join(stream_cipher.encrypt(chunk) for chunk in data_chunks)

# Decryption can also happen chunk by chunk. Here we are decrypting the whole
# stream at once just to check that we get the original data back.
stream_cipher2 = securitylib.advanced_crypto.StreamCipher(encryption_key)
decrypted_data = stream_cipher2.decrypt(encrypted_data)

original_data = ''.join(data_chunks)

assert(decrypted_data == original_data)
```

### Random

Generating random values using a secure source of randomness:

```python
import securitylib

random_bytes = securitylib.random.get_random_bytes(length=16)
random_integer = securitylib.random.get_random_integer(min_result=1000, max_result=9999)
random_string = securitylib.random.get_random_string(length=100, charset='abcdefghijklmnopqrstuvwxyz')
random_GUID = securitylib.random.get_random_GUID()

print(random_bytes, random_integer, random_string, random_GUID)
```

### Passwords

Generating a random password:

```python
import securitylib

password = securitylib.passwords.generate_password(length=12, lower=True, upper=True, digits=True, special=True, ambig=True)

print(password)
```

Getting a password's strength (between 0 and 100):

```python
import securitylib

print(securitylib.passwords.get_password_strength('123456'))
print(securitylib.passwords.get_password_strength('thisismypassword'))
print(securitylib.passwords.get_password_strength('this is my password'))
print(securitylib.passwords.get_password_strength('u6fm08xw@RLs'))
print(securitylib.passwords.get_password_strength('This 1s My P4ssword...'))
```

Validate a user's password against a list of rules:

```python
import securitylib

password = 'this_is_the_users_password'
error_list = securitylib.passwords.validate_password(password, min_length=12, min_lower=1, min_upper=1, min_digits=1, min_special=1, min_strength=50)

print(error_list)
```

            

Raw data

            {
    "_id": null,
    "home_page": "http://oss.sapo.pt/securitylib-python/",
    "name": "securitylib",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.4",
    "maintainer_email": null,
    "keywords": "security, crypto, securitylib",
    "author": "Jose Nuno Pires",
    "author_email": "Jos\u00e9 Nuno Pires <jose-n-pires@telecom.pt>",
    "download_url": null,
    "platform": null,
    "description": "# SAPO Security Lib - Python\n\nThe SAPO Security Lib is a library whose purpose is to provide functions/classes\nthat solve common security related problems, while being easy to use even by\nthose who are not security experts. This repository contains the Python version\nof this library.\n\nOur design principles:\n\n- **Security** \u2014 This is an obvious one, but it is important to explain how it\n  is enforced.\n    - No security primitives were invented, all security sensitive code is based\n      on modern security best-practices (e.g. we use PBKDF2 to derive keys from\n      passwords, we didn't reinvent the wheel).\n    - Very high (near 100%) testing code coverage.\n    - Manual code review by security professionals.\n- **Security by default** \u2014 Using the library with the default parameters should\n  provide enough security for most cases (maybe not military grade top security,\n  but enough for an application like Gmail, for example). Flexibility was even\n  traded in some places for increased security, for example by making it hard\n  (i.e., impossible without messing with the lib code) for someone to use a weak\n  algorithm instead of the default one.\n- **Simple API** \u2014 Unfortunately, the acronyms AES, PBKDF2, HMAC, etc. are\n  cryptic for many developers, and many others know them but might have\n  difficulty knowing when and how to use them. As such, we decided to hide the\n  implementation details in the API function names, resulting in names such as\n  `generate_encryption_key`, `encrypt`, `prepare_password_for_storage`, etc.\n  which most developers are able to understand even if they are not security\n  experts.\n\n\n\nThere are currently 4 modules in this library:\n\n- **crypto** \u2014 Cryptographic functions library.\n- **advanced_crypto** \u2014 Advanced cryptographic functions library.\n- **random** \u2014 Secure generation of random numbers and strings.\n- **passwords** \u2014 Creation and validation of user passwords.\n\nSome examples of use cases for each of these modules are given below.\n\nFor the full documentation of the library, go [here](http://oss.sapo.pt/securitylib-python/).\n\n\n## Discussion\n\nPlease file any bugs you find in our [issue tracker](https://github.com/sapo/securitylib-python).\n\n## Testing\n- tox run\n\n## Building wheel\n- python setup.py bdist\n\n_You should change version in setup.py when upgrading_\n\n## Installation\n\nUpgraded to support from Python 3.5 to 3.12.\nThere are several ways to install SAPO Security Lib.\n\n### Via PyPI\n\nJust run:\n`pip install securitylib`\n\n\n### Via a tarball release\n\n1. Dowload the [tarball](https://github.com/sapo/securitylib-python/archive/1.0.0.tar.gz)\n2. Unpack the tarball\n3. `python setup.py install`\n\n\n## Examples:\n\n### Crypto\n\nGenerating a key for encryption:\n\n```python\nimport securitylib\n\nencryption_key = securitylib.crypto.generate_encryption_key()\n\nprint(encryption_key)\n```\n\nGenerating a key for encryption based on a user's password:\n\n```python\nimport securitylib\n\npassword = 'this_is_the_users_password'\nsalt = securitylib.random.get_random_token()\nencryption_key = securitylib.crypto.generate_encryption_key_from_password(password, salt)\n\nprint(encryption_key)\n```\n\nEncrypting and decrypting data:\n\n```python\nimport securitylib\n\ndata = 'this_is_the_data_we_want_to_encrypt'\nencryption_key = securitylib.crypto.generate_encryption_key()\nauthenticator_key = securitylib.crypto.generate_authenticator_key()\nencrypted_data = securitylib.crypto.encrypt(data, encryption_key, authenticator_key)\ndecrypted_data = securitylib.crypto.decrypt(encrypted_data, encryption_key, authenticator_key)\nassert(decrypted_data == data)\n```\n\n### Advanced Crypto\n\nUsing a stream cipher to encrypt or decrypt a stream:\n\n```python\nimport securitylib\n\ndata_chunks = ['this_is_', 'the_data', '_we', '_want_to_', 'encrypt']\n\nencryption_key = securitylib.crypto.generate_encryption_key()\n\n# Data can be encrypted chunk by chunk\nstream_cipher = securitylib.advanced_crypto.StreamCipher(encryption_key)\nencrypted_data = ''.join(stream_cipher.encrypt(chunk) for chunk in data_chunks)\n\n# Decryption can also happen chunk by chunk. Here we are decrypting the whole\n# stream at once just to check that we get the original data back.\nstream_cipher2 = securitylib.advanced_crypto.StreamCipher(encryption_key)\ndecrypted_data = stream_cipher2.decrypt(encrypted_data)\n\noriginal_data = ''.join(data_chunks)\n\nassert(decrypted_data == original_data)\n```\n\n### Random\n\nGenerating random values using a secure source of randomness:\n\n```python\nimport securitylib\n\nrandom_bytes = securitylib.random.get_random_bytes(length=16)\nrandom_integer = securitylib.random.get_random_integer(min_result=1000, max_result=9999)\nrandom_string = securitylib.random.get_random_string(length=100, charset='abcdefghijklmnopqrstuvwxyz')\nrandom_GUID = securitylib.random.get_random_GUID()\n\nprint(random_bytes, random_integer, random_string, random_GUID)\n```\n\n### Passwords\n\nGenerating a random password:\n\n```python\nimport securitylib\n\npassword = securitylib.passwords.generate_password(length=12, lower=True, upper=True, digits=True, special=True, ambig=True)\n\nprint(password)\n```\n\nGetting a password's strength (between 0 and 100):\n\n```python\nimport securitylib\n\nprint(securitylib.passwords.get_password_strength('123456'))\nprint(securitylib.passwords.get_password_strength('thisismypassword'))\nprint(securitylib.passwords.get_password_strength('this is my password'))\nprint(securitylib.passwords.get_password_strength('u6fm08xw@RLs'))\nprint(securitylib.passwords.get_password_strength('This 1s My P4ssword...'))\n```\n\nValidate a user's password against a list of rules:\n\n```python\nimport securitylib\n\npassword = 'this_is_the_users_password'\nerror_list = securitylib.passwords.validate_password(password, min_length=12, min_lower=1, min_upper=1, min_digits=1, min_special=1, min_strength=50)\n\nprint(error_list)\n```\n",
    "bugtrack_url": null,
    "license": "MIT License",
    "summary": "SAPO Security Lib - Python",
    "version": "1.0.9",
    "project_urls": {
        "Homepage": "https://github.com/sapo/securitylib-python",
        "Issues": "https://github.com/sapo/securitylib-python/issues"
    },
    "split_keywords": [
        "security",
        " crypto",
        " securitylib"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e0e7361a83ae37d015c1665d9eb19dacf838ec20e7c1b9bf35e65d66a27b9a25",
                "md5": "00649505f93fd6af50a01eba4415e18a",
                "sha256": "56d6b8494c66393b820fa0866d2d52d76f930e75bccc06e48e0d0f503c88b018"
            },
            "downloads": -1,
            "filename": "securitylib-1.0.9-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "00649505f93fd6af50a01eba4415e18a",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.4",
            "size": 266411,
            "upload_time": "2024-12-11T09:34:15",
            "upload_time_iso_8601": "2024-12-11T09:34:15.784967Z",
            "url": "https://files.pythonhosted.org/packages/e0/e7/361a83ae37d015c1665d9eb19dacf838ec20e7c1b9bf35e65d66a27b9a25/securitylib-1.0.9-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-12-11 09:34:15",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "sapo",
    "github_project": "securitylib-python",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [
        {
            "name": "pycryptodome",
            "specs": [
                [
                    ">=",
                    "3.19.1"
                ]
            ]
        },
        {
            "name": "pytest",
            "specs": [
                [
                    ">=",
                    "8"
                ]
            ]
        },
        {
            "name": "pytest-sugar",
            "specs": []
        }
    ],
    "tox": true,
    "lcname": "securitylib"
}
        
Elapsed time: 0.38232s