| Start |
|---|
| Secure Password Hashing The PBKDF2 algorithm (opens new window) exposed by PBKDF2 can work with any digest algorithm, the above example uses SHA256 which is usually recommended. The random salt should be stored along with the hashed password, you will need it again in order to compare an entered password to the stored hash. It is essential that each password is hashed with a different salt. As to the number of rounds, it is recommended to set it as high as possible for your application (opens new window). If you want the result in hexadecimal, you can use the Note: While PBKDF2 isn't bad, bcrypt (opens new window) and especially scrypt (opens new window) are considered stronger against brute-force attacks. Neither is part of the Python standard library at the moment. |
| Calculating a Message Digest The Note that you can call |
| Available Hashing Algorithms
The returned list will vary according to platform and interpreter; make sure you check your algorithm is available. There are also some algorithms that are guaranteed to be available on all platforms and interpreters, which are available using |
| File Hashing A hash is a function that converts a variable length sequence of bytes to a fixed length sequence. Hashing files can be advantageous for many reasons. Hashes can be used to check if two files are identical or verify that the contents of a file haven't been corrupted or changed. You can use For larger files, a buffer of fixed length can be used: |
| Symmetric encryption using pycrypto Python's built-in crypto functionality is currently limited to hashing. Encryption requires a third-party module like pycrypto (opens new window). For example, it provides the AES algorithm (opens new window) which is considered state of the art for symmetric encryption. The following code will encrypt a given message using a passphrase: The AES algorithm takes three parameters: encryption key, initialization vector (IV) and the actual message to be encrypted. If you have a randomly generated AES key then you can use that one directly and merely generate a random initialization vector. A passphrase doesn't have the right size however, nor would it be recommendable to use it directly given that it isn't truly random and thus has comparably little entropy. Instead, we use the built-in implementation of the PBKDF2 algorithm (opens new window) to generate a 128 bit initialization vector and 256 bit encryption key from the password. Note the random salt which is important to have a different initialization vector and key for each message encrypted. This ensures in particular that two equal messages won't result in identical encrypted text, but it also prevents attackers from reusing work spent guessing one passphrase on messages encrypted with another passphrase. This salt has to be stored along with the encrypted message in order to derive the same initialization vector and key for decrypting. The following code will decrypt our message again: |
| Generating RSA signatures using pycrypto RSA (opens new window) can be used to create a message signature. A valid signature can only be generated with access to the private RSA key, validating on the other hand is possible with merely the corresponding public key. So as long as the other side knows your public key they can verify the message to be signed by you and unchanged - an approach used for email for example. Currently, a third-party module like pycrypto (opens new window) is required for this functionality. Verifying the signature works similarly but uses the public key rather than the private key: Note: The above examples use PKCS#1 v1.5 signing algorithm which is very common. pycrypto also implements the newer PKCS#1 PSS algorithm, replacing |
| Asymmetric RSA encryption using pycrypto Asymmetric encryption has the advantage that a message can be encrypted without exchanging a secret key with the recipient of the message. The sender merely needs to know the recipients public key, this allows encrypting the message in such a way that only the designated recipient (who has the corresponding private key) can decrypt it. Currently, a third-party module like pycrypto (opens new window) is required for this functionality. The recipient can decrypt the message then if they have the right private key: Note: The above examples use PKCS#1 OAEP encryption scheme. pycrypto also implements PKCS#1 v1.5 encryption scheme, this one is not recommended for new protocols however due to known caveats (opens new window). Syntax
RemarksMany of the methods in |
Read Article
https://develop.horje.com/learn/1434/reference