Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds namespace support #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
[![TravisCI](https://img.shields.io/travis/charlesportwoodii/php-argon2-ext.svg?style=flat-square "TravisCI")](https://travis-ci.org/charlesportwoodii/php-argon2-ext)
[![License](https://img.shields.io/badge/license-BSD-orange.svg?style=flat-square "License")](https://github.com/charlesportwoodii//php-argon2-ext/blob/master/LICENSE.md)


This PHP7 extension provides a simplified interface to the [Argon2](https://github.com/P-H-C/phc-winner-argon2) algorithm, the winner of the [Password Hashing Competition](https://password-hashing.net/). Argon2 is considered the successor to bcrypt/scrypt/pbkdf methods of securely hasing passwords. This project is in no way associated with or endorsed by the PHC team.

> Note this is extension is only compatible with PHP7+. Support for lower versions of PHP will not be considered.

> Note this extension provides functionality identical to Libsodium's `sodium_crypto_pwhash` and `sodium_crypto_pwhash_str` functions.

## Building

```bash
Expand Down Expand Up @@ -69,6 +70,7 @@ The constant `HASH_ARGON2ID` can also be aliased by `HASH_ARGON2`.
### Hash Generation
```php
argon2_hash(string $string [, const $algorithm = HASH_ARGON2ID] [, array $options ] [, bool $raw = false ]);
\Argon2\Hash(string $string [, const $algorithm = HASH_ARGON2ID] [, array $options ] [, bool $raw = false ]);
```

Hashes can be generated by running `argon2_hash()` with the string you want to see hashed. Without any additional arguements, the hash generated will have the following options. These defaults are based upon the Argon2 specification as good minimums. You are encouraged to run `make bench` against the `ext/argon2` to determine what are good defaults for your system.
Expand Down Expand Up @@ -104,13 +106,15 @@ $argon2i$v=19$m=65536,t=3,p=1$aUEvQlU2NTRwcHhVS0hqMg$+5h0P5YlWCJDKyZknJ0sAyqQtZj
### Validating Hashes
```php
argon2_verify(string $string, string $hash);
\Argon2\Verify(string $string, string $hash);
```

Hashes can be verified by running `argon2_verify()` with the string string and string hash generated by `argon2_hash`. This function will return either `true` or `false` depending upon if the hash is valid or not. If the `hash` provided isn't a valid argon2 hash, `false` will be returned, an an `E_WARNING` will be raised.

### Retrieving Hash Information
```php
argon2_get_info(string $hash);
\Argon2\Info(string $hash);
```

To retrieve information about an existing Argon2 hash, run `argon2_get_info()`. This function will return an array containing the algorithm name, and the options used for hash generation.
Expand Down
10 changes: 10 additions & 0 deletions argon2.c
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ const zend_function_entry argon2_functions[] = {
PHP_FE(argon2_hash, arginfo_argon2_hash)
PHP_FE(argon2_verify, arginfo_argon2_verify)
PHP_FE(argon2_get_info, arginfo_argon2_get_info)

ZEND_NS_NAMED_FE("Argon2", Hash, ZEND_FN(argon2_hash), arginfo_argon2_hash)
ZEND_NS_NAMED_FE("Argon2", Verify, ZEND_FN(argon2_verify), arginfo_argon2_verify)
ZEND_NS_NAMED_FE("Argon2", Info, ZEND_FN(argon2_get_info), arginfo_argon2_get_info)

PHP_FE_END
};
/* }}} */
Expand All @@ -346,6 +351,11 @@ PHP_MINIT_FUNCTION(argon2)
REGISTER_LONG_CONSTANT("HASH_ARGON2ID", EXT_HASH_ARGON2ID, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("HASH_ARGON2", EXT_HASH_ARGON2, CONST_CS | CONST_PERSISTENT);

REGISTER_LONG_CONSTANT("Argon2\\HASH_ARGON2D", EXT_HASH_ARGON2D, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("Argon2\\HASH_ARGON2I", EXT_HASH_ARGON2I, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("Argon2\\HASH_ARGON2ID", EXT_HASH_ARGON2ID, CONST_CS | CONST_PERSISTENT);
REGISTER_LONG_CONSTANT("Argon2\\HASH_ARGON2", EXT_HASH_ARGON2, CONST_CS | CONST_PERSISTENT);

return SUCCESS;
}
/* }}} */
Expand Down
8 changes: 8 additions & 0 deletions tests/argon2_001.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,16 @@ var_dump(HASH_ARGON2);
var_dump(HASH_ARGON2ID);
var_dump(HASH_ARGON2I);
var_dump(HASH_ARGON2D);
var_dump(\Argon2\HASH_ARGON2);
var_dump(\Argon2\HASH_ARGON2ID);
var_dump(\Argon2\HASH_ARGON2I);
var_dump(\Argon2\HASH_ARGON2D);
--EXPECT--
int(2)
int(2)
int(1)
int(0)
int(2)
int(2)
int(1)
int(0)
22 changes: 22 additions & 0 deletions tests/argon2_012.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
Tests Argon2 namespace support
--FILE--
<?php
$hash = \Argon2\Hash('password');
var_dump(\Argon2\Verify('password', $hash));
var_dump(\Argon2\Info($hash));
--EXPECT--
bool(true)
array(2) {
["algorithm"]=>
string(8) "argon2id"
["options"]=>
array(3) {
["m_cost"]=>
int(65536)
["t_cost"]=>
int(3)
["threads"]=>
int(1)
}
}