Replace old-style C conversions in entropy estimator (#150)

This commit is contained in:
Tarquin Winot 2017-01-10 23:37:58 +01:00 committed by Jonathan White
parent ff6d78244b
commit f85198c60f
2 changed files with 14 additions and 13 deletions

View File

@ -1,4 +1,4 @@
/********************************************************************************** /**********************************************************************************
* C implementation of the zxcvbn password strength estimation method. * C implementation of the zxcvbn password strength estimation method.
* Copyright (c) 2015, Tony Evans * Copyright (c) 2015, Tony Evans
* All rights reserved. * All rights reserved.
@ -228,12 +228,13 @@ static void AddMatchRepeats(ZxcMatch_t **Result, ZxcMatch_t *Match, const uint8_
while(MaxLen >= (Len * RepeatCount)) while(MaxLen >= (Len * RepeatCount))
{ {
if (strncmp((const char *)Passwd, (const char *)Rpt, Len) == 0) if (strncmp(reinterpret_cast<const char *>(Passwd),
reinterpret_cast<const char *>(Rpt), Len) == 0)
{ {
/* Found a repeat */ /* Found a repeat */
ZxcMatch_t *p = AllocMatch(); ZxcMatch_t *p = AllocMatch();
p->Entrpy = Match->Entrpy + log(RepeatCount); p->Entrpy = Match->Entrpy + log(RepeatCount);
p->Type = (ZxcTypeMatch_t)(Match->Type + MULTIPLE_MATCH); p->Type = static_cast<ZxcTypeMatch_t>(Match->Type + MULTIPLE_MATCH);
p->Length = Len * RepeatCount; p->Length = Len * RepeatCount;
p->Begin = Match->Begin; p->Begin = Match->Begin;
AddResult(Result, p, MaxLen); AddResult(Result, p, MaxLen);
@ -617,7 +618,7 @@ static void DictionaryEntropy(ZxcMatch_t *m, DictMatchInfo_t *Extra, const uint8
e += d; e += d;
} }
/* Add entropy due to word's rank */ /* Add entropy due to word's rank */
e += log((double)Extra->Rank); e += log(static_cast<double>(Extra->Rank));
m->Entrpy = e; m->Entrpy = e;
} }
@ -794,7 +795,7 @@ static void UserMatch(ZxcMatch_t **Result, const char *Words[], const uint8_t *P
int Caps = 0; int Caps = 0;
int Lowers = 0; int Lowers = 0;
int Leets = 0; int Leets = 0;
const uint8_t *Wrd = (const uint8_t *)(Words[Rank]); const uint8_t *Wrd = reinterpret_cast<const uint8_t *>(Words[Rank]);
const uint8_t *Pwd = Passwd; const uint8_t *Pwd = Passwd;
memset(Extra.Leeted, 0, sizeof Extra.Leeted); memset(Extra.Leeted, 0, sizeof Extra.Leeted);
memset(Extra.UnLeet, 0, sizeof Extra.UnLeet); memset(Extra.UnLeet, 0, sizeof Extra.UnLeet);
@ -1169,7 +1170,7 @@ static void SpatialMatch(ZxcMatch_t **Result, const uint8_t *Passwd, int Start,
int i, j, s; int i, j, s;
double Degree, Entropy; double Degree, Entropy;
ZxcMatch_t *p; ZxcMatch_t *p;
Degree = (k->NumNear-1) - (double)k->NumBlank / (double)k->NumKeys; Degree = (k->NumNear-1) - static_cast<double>(k->NumBlank) / static_cast<double>(k->NumKeys);
s = k->NumKeys; s = k->NumKeys;
if (k->Shifts) if (k->Shifts)
s *= 2; s *= 2;
@ -1405,13 +1406,13 @@ static void RepeatMatch(ZxcMatch_t **Result, const uint8_t *Passwd, int Start, i
int RepeatCount = 2; int RepeatCount = 2;
while(MaxLen >= (Len * RepeatCount)) while(MaxLen >= (Len * RepeatCount))
{ {
if (strncmp((const char *)Passwd, (const char *)Rpt, Len) == 0) if (strncmp(reinterpret_cast<const char *>(Passwd), reinterpret_cast<const char *>(Rpt), Len) == 0)
{ {
/* Found a repeat */ /* Found a repeat */
int c = Cardinality(Passwd, Len); int c = Cardinality(Passwd, Len);
ZxcMatch_t *p = AllocMatch(); ZxcMatch_t *p = AllocMatch();
p->Entrpy = log((double)c) * Len + log(RepeatCount); p->Entrpy = log(static_cast<double>(c)) * Len + log(RepeatCount);
p->Type = (ZxcTypeMatch_t)(BRUTE_MATCH + MULTIPLE_MATCH); p->Type = static_cast<ZxcTypeMatch_t>(BRUTE_MATCH + MULTIPLE_MATCH);
p->Length = Len * RepeatCount; p->Length = Len * RepeatCount;
p->Begin = Start; p->Begin = Start;
AddResult(Result, p, MaxLen); AddResult(Result, p, MaxLen);
@ -1527,7 +1528,7 @@ static void SequenceMatch(ZxcMatch_t **Result, const uint8_t *Passwd, int Start,
p->Type = SEQUENCE_MATCH; p->Type = SEQUENCE_MATCH;
p->Begin = Start; p->Begin = Start;
p->Length = i; p->Length = i;
p->Entrpy = e + log((double)i); p->Entrpy = e + log(static_cast<double>(i));
AddMatchRepeats(Result, p, Pwd, MaxLen); AddMatchRepeats(Result, p, Pwd, MaxLen);
AddResult(Result, p, MaxLen); AddResult(Result, p, MaxLen);
} }
@ -1578,13 +1579,13 @@ double ZxcvbnMatch(const char *Pwd, const char *UserDict[], ZxcMatch_t **Info)
Node_t *Np; Node_t *Np;
double e; double e;
int Len = strlen(Pwd); int Len = strlen(Pwd);
const uint8_t *Passwd = (const uint8_t *)Pwd; const uint8_t *Passwd = reinterpret_cast<const uint8_t *>(Pwd);
uint8_t *RevPwd; uint8_t *RevPwd;
/* Create the paths */ /* Create the paths */
Node_t *Nodes = MallocFn(Node_t, Len+1); Node_t *Nodes = MallocFn(Node_t, Len+1);
memset(Nodes, 0, (Len+1) * sizeof *Nodes); memset(Nodes, 0, (Len+1) * sizeof *Nodes);
i = Cardinality(Passwd, Len); i = Cardinality(Passwd, Len);
e = log((double)i); e = log(static_cast<double>(i));
/* Do matching for all parts of the password */ /* Do matching for all parts of the password */
for(i = 0; i < Len; ++i) for(i = 0; i < Len; ++i)

View File

@ -1,4 +1,4 @@
#ifndef ZXCVBN_H_F98183CE2A01_INCLUDED #ifndef ZXCVBN_H_F98183CE2A01_INCLUDED
#define ZXCVBN_H_F98183CE2A01_INCLUDED #define ZXCVBN_H_F98183CE2A01_INCLUDED
/********************************************************************************** /**********************************************************************************
* C implementation of the zxcvbn password strength estimation method. * C implementation of the zxcvbn password strength estimation method.