security - Constant-time string comparison function -


to compare 2 strings, use strcmp or 1 of variants. however, because strcmp take longer if more characters match, vulnerable timing attacks. there constant-time string comparison function in standard library on windows?

i don't think windows nor visual studio has such functions.

at least simple strcmp can whip yourself.

if care equality:

int strctcmp(const char*a, const char*b) {   int r = 0;   (; *a && *b; ++a, ++b)   {     r |= *a != *b;   }   return r; } 

if need sortable results and need process of longest string:

int strctcmp(const char*a, const char*b) {   int r = 0, c;   (;;)   {     c = *a - *b;     if (!r) r = c;     if (!*a && !*b) break;     if (*a) ++a;     if (*b) ++b;   }   return r; } 

these not perfect timing wise should more enough network based.


Comments

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -