Welcome to mirror list, hosted at ThFree Co, Russian Federation.

entropy-meter.cpp « utils - github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a5e6e50e9208ed76f65492a9744e2fbab81c61eb (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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/*
Part of this code come from zxcvbn-c example.
Copyright (c) 2015, Tony Evans
Copyright (c) 2016, KeePassXC Team

See zxcvbn/zxcvbn.cpp for complete COPYRIGHT Notice
*/

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "zxcvbn/zxcvbn.h"

/* For pre-compiled headers under windows */
#ifdef _WIN32
#ifndef __MINGW32__
#include "stdafx.h"
#endif
#endif

static void calculate(const char *pwd, int advanced)
{
    double e;
    int len = strlen(pwd);
    if (advanced == 0){
      e = ZxcvbnMatch(pwd, 0, 0);
      printf("Pass '%s' \tLength %d\tEntropy %.3f\tLog10 %.3f\n", pwd, len, e, e * 0.301029996);
    } else {
      int ChkLen;
      ZxcMatch_t *info, *p;
      double m = 0.0;
      e = ZxcvbnMatch(pwd, 0, &info);
      for(p = info; p; p = p->Next) {
          m += p->Entrpy;
      }
      m = e - m;
      printf("Pass '%s' \tLength %d\tEntropy %.3f\tLog10 %.3f\n  Multi-word extra bits %.1f\n", pwd, len, e, e * 0.301029996, m);
      p = info;
      ChkLen = 0;
      while(p) {
        int n;
        switch(static_cast<int>(p->Type))
        {
            case BRUTE_MATCH:                     printf("  Type: Bruteforce     ");   break;
            case DICTIONARY_MATCH:                printf("  Type: Dictionary     ");   break;
            case DICT_LEET_MATCH:                 printf("  Type: Dict+Leet      ");   break;
            case USER_MATCH:                      printf("  Type: User Words     ");   break;
            case USER_LEET_MATCH:                 printf("  Type: User+Leet      ");   break;
            case REPEATS_MATCH:                   printf("  Type: Repeated       ");   break;
            case SEQUENCE_MATCH:                  printf("  Type: Sequence       ");   break;
            case SPATIAL_MATCH:                   printf("  Type: Spatial        ");   break;
            case DATE_MATCH:                      printf("  Type: Date           ");   break;
            case BRUTE_MATCH+MULTIPLE_MATCH:      printf("  Type: Bruteforce(Rep)");   break;
            case DICTIONARY_MATCH+MULTIPLE_MATCH: printf("  Type: Dictionary(Rep)");   break;
            case DICT_LEET_MATCH+MULTIPLE_MATCH:  printf("  Type: Dict+Leet(Rep) ");   break;
            case USER_MATCH+MULTIPLE_MATCH:       printf("  Type: User Words(Rep)");   break;
            case USER_LEET_MATCH+MULTIPLE_MATCH:  printf("  Type: User+Leet(Rep) ");   break;
            case REPEATS_MATCH+MULTIPLE_MATCH:    printf("  Type: Repeated(Rep)  ");   break;
            case SEQUENCE_MATCH+MULTIPLE_MATCH:   printf("  Type: Sequence(Rep)  ");   break;
            case SPATIAL_MATCH+MULTIPLE_MATCH:    printf("  Type: Spatial(Rep)   ");   break;
            case DATE_MATCH+MULTIPLE_MATCH:       printf("  Type: Date(Rep)      ");   break;

            default:                printf("  Type: Unknown%d ", p->Type);   break;
        }
        ChkLen += p->Length;
        printf("  Length %d  Entropy %6.3f (%.2f) ", p->Length, p->Entrpy, p->Entrpy * 0.301029996);
        for(n = 0; n < p->Length; ++n, ++pwd) {
          printf("%c", *pwd);
        }
        printf("\n");
        p = p->Next;
      }
      ZxcvbnFreeInfo(info);
      if (ChkLen != len)
          printf("*** Password length (%d) != sum of length of parts (%d) ***\n", len, ChkLen);
    }
}

int main(int argc, char **argv)
{
    printf("KeePassXC Entropy Meter, based on zxcvbn-c.\nEnter your password below or pass it as argv\n");
    printf("  Usage: entropy-meter [-a] [pwd1 pwd2 ...]\n> ");
    int i, advanced;
    if ((argc > 1) && (argv[1][0] == '-') && (!strcmp(argv[1], "-a")))
    {
      advanced = 1;
    }
    i = 2;
    if (i >= argc)
    {
        /* No test passwords on command line, so get them from stdin */
        char line[500];
        while(fgets(line, sizeof line, stdin))
        {
            /* Drop the trailing newline character */
            for(i = 0; i < static_cast<int>(sizeof line - 1); ++i)
            {
                if (line[i] < ' ')
                {
                    line[i] = 0;
                    break;
                }
            }
            if (line[0])
                calculate(line,advanced);
                printf("> ");
        }
    }
    else
    {
        /* Do the test passwords on the command line */
        for(; i < argc; ++i)
        {
            calculate(argv[i],advanced);
        }
    }
    return 0;
}