2017 © Pedro Peláez
 

library justencrypt

image

btccom/justencrypt

  • Thursday, March 8, 2018
  • by rubensayshi
  • Repository
  • 7 Watchers
  • 3 Stars
  • 2,854 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 7 Versions
  • 67 % Grown

The README.md

### JustEncrypt PHP library

Build Status Code Coverage Scrutinizer Code Quality Latest Stable Version, (*1)

PHP Support

This library supports PHP versions 5.6 through 7.1. Using PHP 7.1 is highly recommended, because the OpenSSL extension now supports the algorithm we use. Otherwise, you fall back on a much slower implementation in native PHP., (*2)

Usage

Key Derivation

The following example shows simple key derivation from a password/salt/iterations Simple derivation example, (*3)

KeyDerivation::generateSalt() will return a salt using the current default. Encryption::encrypt will generate the salt/iterations above using library defaults, (*4)

Encryption

An encrypted blob is the concatenation of saltLen (uint8) || salt || iv || ct || tag The serialized parameters allow us to decrypt on any machine knowing only the password., (*5)

Simple encryption example Advanced example with subkeys & root key recovery, (*6)

Encryption::encrypt returns an EncryptedBlob, which encapsulates key derivation data and ciphertext details. It also exposes useful methods for the ciphertext:, (*7)

  • $blob->getBinary() - returns raw binary for encrypted blob, for base64, etc.
  • $blob->getMnemonic() - returns the Encryption Mnemonic (see below)
  • $blob->getBuffer() - returns a Buffer, useful for converting to hex, etc

Encryption Mnemonic

To make the result of encrypt human readable (so it is easier to write down) it's possible to encode it as an mnemonic. We're using the Bitcoin BIP39 way of encoding entropy to mnemonic, but ignoring the (weak) password protection BIP39 originally had. We also ensure the data is padded correctly., (*8)

$encrypted->getMnemonic() calls the EncryptedMnemonic class to produce Encryption Mnemonic example, (*9)

Choosing iterations

The default iterations is justencrypt.KeyDerivation.defaultIterations and is set to 35000, this is a number that should remain secure enough for a while when using a password.
If you don't pass in the iterations argument it will default to this., (*10)

If you're encrypting with a CSPRNG generated random byte string as the password then you can use the same code, except in that case setting the iterations to 1 is secure as there's no need to stretch the password.
You can use justencrypt.KeyDerivation.subkeyIterations in that case to make it clear what your intentions are., (*11)

This type of usage is demonstrated in this example, (*12)

Development / Contributing

Please see CONTRIBUTING.md for details on contributing., (*13)

License

JustEncrypt is released under the terms of the MIT license. See LICENCE.md for more information or see http://opensource.org/licenses/MIT., (*14)

The Versions