summaryrefslogtreecommitdiff
path: root/slowequals.c
blob: 48e488e4eb8c35b64f8ee2c413300afd48da20a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <string.h>

/* Implements a constant time version of strcmp()
 * Will return 1 if a and b are equal, 0 if they are not */
int slow_equals(const char* a, const char* b)
{
    size_t lena, lenb, diff, i;
    lena = strlen(a);
    lenb = strlen(b);
    diff = strlen(a) ^ strlen(b);

    for(i=0; i<lena && i<lenb; i++)
    {
        diff |= a[i] ^ b[i];
    }
    if (diff == 0)
    {
        return 1;
    }
    else
    {
        return 0;
    }
}