From 1107d7bcb4c4da29e0df4cff00ea8e6d650c0f2f Mon Sep 17 00:00:00 2001 From: Christoph Hilchenbach Date: Fri, 27 Dec 2019 16:27:39 +0100 Subject: [PATCH] changed interface --- .gitignore | 2 +- README.md | 9 ++++----- bcrypt.cbp | 47 +++++++++++++++++++++++++++++++++++++++++++++++ include/bcrypt.h | 12 ++---------- src/bcrypt.cpp | 16 +++++++++------- src/blf.h | 2 -- src/blowfish.cpp | 2 -- src/openbsd.cpp | 5 ++--- src/openbsd.h | 5 ++--- test/main.cpp | 7 +++---- 10 files changed, 70 insertions(+), 37 deletions(-) create mode 100644 bcrypt.cbp diff --git a/.gitignore b/.gitignore index afa17fb..c6c1087 100644 --- a/.gitignore +++ b/.gitignore @@ -34,5 +34,5 @@ bin obj # IDE Files -*.cbp +*.layout diff --git a/README.md b/README.md index 22c23e2..bcafb07 100644 --- a/README.md +++ b/README.md @@ -11,22 +11,21 @@ Here an example how to use this wrapper class #include -using namespace bcrypt; - int main() { std::string password = "top_secret"; - std::string hash = BCrypt::generateHash(password); + std::string hash = bcrypt::generateHash(password); std::cout << "Hash: " << hash << std::endl; - std::cout << "\"" << password << "\" : " << BCrypt::validatePassword(password,hash) << std::endl; - std::cout << "\"wrong\" : " << BCrypt::validatePassword("wrong",hash) << std::endl; + std::cout << "\"" << password << "\" : " << bcrypt::validatePassword(password,hash) << std::endl; + std::cout << "\"wrong\" : " << bcrypt::validatePassword("wrong",hash) << std::endl; return 0; } + ``` output: diff --git a/bcrypt.cbp b/bcrypt.cbp new file mode 100644 index 0000000..7ef8f0e --- /dev/null +++ b/bcrypt.cbp @@ -0,0 +1,47 @@ + + + + + + diff --git a/include/bcrypt.h b/include/bcrypt.h index 298c001..baf716c 100644 --- a/include/bcrypt.h +++ b/include/bcrypt.h @@ -6,16 +6,8 @@ namespace bcrypt { -class BCrypt { -public: - static std::string generateHash(const std::string & password , unsigned rounds = 10 ); - static bool validatePassword(const std::string & password, const std::string & hash); - -}; - - -int bcrypt_newhash(const char *pass, int log_rounds, char *hash, size_t hashlen); -int bcrypt_checkpass(const char *pass, const char *goodhash); + std::string generateHash(const std::string & password , unsigned rounds = 10 ); + bool validatePassword(const std::string & password, const std::string & hash); } diff --git a/src/bcrypt.cpp b/src/bcrypt.cpp index 02992b7..314424b 100644 --- a/src/bcrypt.cpp +++ b/src/bcrypt.cpp @@ -45,8 +45,7 @@ #include #include -namespace bcrypt -{ + /* This implementation is adaptable to current computing power. * You can have up to 2^31 rounds which should be enough for some @@ -416,15 +415,18 @@ bcrypt_gensalt(uint8_t log_rounds) bool init = false; -std::string BCrypt::generateHash(const std::string & password , unsigned rounds) +namespace bcrypt +{ + +std::string generateHash(const std::string & password, unsigned rounds) { if( !init ) { - arc4random_init(); - init = true; + arc4random_init(); + init = true; } - char hash[61]={0}; + char hash[61]= {0}; int result = bcrypt_newhash( password.c_str(), rounds, hash, sizeof(hash)); if( !result ) @@ -433,7 +435,7 @@ std::string BCrypt::generateHash(const std::string & password , unsigned rounds) return(""); } -bool BCrypt::validatePassword(const std::string & password, const std::string & hash) +bool validatePassword(const std::string & password, const std::string & hash) { return !bcrypt_checkpass(password.c_str(), hash.c_str() ); } diff --git a/src/blf.h b/src/blf.h index 6387ed9..5390b43 100644 --- a/src/blf.h +++ b/src/blf.h @@ -37,7 +37,6 @@ #include -namespace bcrypt { /* Schneier states the maximum key length to be 56 bytes. @@ -88,6 +87,5 @@ void blf_ecb_decrypt(blf_ctx *, uint8_t *, uint32_t); void blf_cbc_encrypt(blf_ctx *, uint8_t *, uint8_t *, uint32_t); void blf_cbc_decrypt(blf_ctx *, uint8_t *, uint8_t *, uint32_t); -} #endif diff --git a/src/blowfish.cpp b/src/blowfish.cpp index 140712b..c7d8fab 100644 --- a/src/blowfish.cpp +++ b/src/blowfish.cpp @@ -41,7 +41,6 @@ #include "blf.h" -namespace bcrypt { /* Function for Feistel Networks */ @@ -674,4 +673,3 @@ blf_cbc_decrypt(blf_ctx *c, uint8_t *iva, uint8_t *data, uint32_t len) } -} diff --git a/src/openbsd.cpp b/src/openbsd.cpp index 6e277fe..644900f 100644 --- a/src/openbsd.cpp +++ b/src/openbsd.cpp @@ -3,8 +3,7 @@ #include #include -namespace bcrypt -{ + void arc4random_buf(void *buf, size_t nbytes) { for( size_t n = 0; n < nbytes; ++ n) @@ -22,4 +21,4 @@ void explicit_bzero(void *b, size_t len) dont_optimize = dont_optimize; } -} + diff --git a/src/openbsd.h b/src/openbsd.h index ab2ab65..e6e2029 100644 --- a/src/openbsd.h +++ b/src/openbsd.h @@ -3,10 +3,9 @@ #include -namespace bcrypt -{ + void arc4random_buf(void *buf, size_t nbytes); void explicit_bzero(void *b, size_t len); void arc4random_init(void); -} + #endif // ARC4RANDOM_H_INCLUDED diff --git a/test/main.cpp b/test/main.cpp index 055252f..0c64bec 100644 --- a/test/main.cpp +++ b/test/main.cpp @@ -2,19 +2,18 @@ #include #include -using namespace bcrypt; int main() { std::string password = "top_secret"; - std::string hash = BCrypt::generateHash(password); + std::string hash = bcrypt::generateHash(password); std::cout << "Hash: " << hash << std::endl; - std::cout << "\"" << password << "\" : " << BCrypt::validatePassword(password,hash) << std::endl; - std::cout << "\"wrong\" : " << BCrypt::validatePassword("wrong",hash) << std::endl; + std::cout << "\"" << password << "\" : " << bcrypt::validatePassword(password,hash) << std::endl; + std::cout << "\"wrong\" : " << bcrypt::validatePassword("wrong",hash) << std::endl; return 0; }