Skip to content

Commit

Permalink
Add function bcrypt_needs_rehash
Browse files Browse the repository at this point in the history
The function can be used to check whether or not a given hash is valid,
and maches a given cost.
  • Loading branch information
lassir committed May 31, 2014
1 parent ac69178 commit 03f2712
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 2 deletions.
4 changes: 3 additions & 1 deletion bcrypt.inc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* The MIT License (MIT)
* Copyright (c) 2013 Lassi R.
* Copyright (c) 2014 Lassi R.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
Expand All @@ -26,9 +26,11 @@
#define bcrypt_included

#define BCRYPT_HASH_LENGTH 61
#define BCRYPT_PLUGIN_VERSION "2.2"

native bcrypt_hash(key[], cost = 12, callback_name[], callback_format[] = "", {Float, _}:...);
native bcrypt_check(password[], hash[], callback_name[], callback_format[] = "", {Float, _}:...);
native bcrypt_get_hash(dest[]);
native bool:bcrypt_is_equal();
native bool:bcrypt_needs_rehash(hash[], cost);
native bcrypt_set_thread_limit(value);
1 change: 1 addition & 0 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ AMX_NATIVE_INFO PluginNatives [] =
DEFINE_NATIVE(bcrypt_check)
DEFINE_NATIVE(bcrypt_get_hash)
DEFINE_NATIVE(bcrypt_is_equal)
DEFINE_NATIVE(bcrypt_needs_rehash)
DEFINE_NATIVE(bcrypt_set_thread_limit)
{ 0, 0 }
};
Expand Down
2 changes: 1 addition & 1 deletion src/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
#include "SDK/amx/amx.h"
#include "SDK/plugincommon.h"

#define BCRYPT_VERSION "v2.1.2"
#define BCRYPT_VERSION "v2.2"

#endif
25 changes: 25 additions & 0 deletions src/natives.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <thread>
#include <cstring>

#include "plugin.h"
#include "natives.h"
Expand Down Expand Up @@ -96,6 +97,30 @@ DECLARE_NATIVE(native::bcrypt_is_equal)
return (int)plugin::get()->get_active_match();
}

// native bool:bcrypt_needs_rehash(hash[], cost);
DECLARE_NATIVE(native::bcrypt_needs_rehash)
{
if (params[0] != sizeof(cell) * 2)
{
plugin::printf("plugin.bcrypt: bcrypt_needs_rehash: Invalid number of parameters (2 expected)");
}

char *hash = NULL;
unsigned expected_cost;

amx_StrParam(amx, params[1], hash);
expected_cost = static_cast<unsigned>(params[2]);

if (hash[0] != '$' || hash[1] != '2' || hash[2] != 'y' || hash[3] != '$' || hash[6] != '$' || strlen(hash) != 60)
return 1;

unsigned cost = (hash[4] - '0') * 10 + (hash[5] - '0');
if (cost != expected_cost)
return 1;

return 0;
}

// native bcrypt_set_thread_limit(value);
DECLARE_NATIVE(native::bcrypt_set_thread_limit)
{
Expand Down
1 change: 1 addition & 0 deletions src/natives.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ namespace native
DECLARE_NATIVE(bcrypt_get_hash);
DECLARE_NATIVE(bcrypt_check);
DECLARE_NATIVE(bcrypt_is_equal);
DECLARE_NATIVE(bcrypt_needs_rehash);
DECLARE_NATIVE(bcrypt_set_thread_limit);
}

Expand Down

0 comments on commit 03f2712

Please sign in to comment.