File: md5crypt.cpp

package info (click to toggle)
qca2 2.3.10-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,884 kB
  • sloc: cpp: 59,224; ansic: 814; perl: 133; sh: 89; makefile: 34
file content (193 lines) | stat: -rw-r--r-- 6,151 bytes parent folder | download | duplicates (3)
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/*
  Copyright (C) 2007 Carlo Todeschini - Metarete s.r.l. <info@metarete.it>

  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 the Software without restriction, including without limitation the rights
  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  copies of the Software, and to permit persons to whom the Software is
  furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
  AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/*
  Algorithm inspired by Vladimir Silva's "Secure Java apps on Linux using
  MD5 crypt" article
  (http://www-128.ibm.com/developerworks/linux/library/l-md5crypt/)
*/

#include <QCoreApplication>
#include <QtCrypto>
#include <QtDebug>
#include <cstdio>

#ifdef QT_STATICPLUGIN
#include "import_plugins.h"
#endif

QString to64(long v, int size)
{
    // Character set of the encrypted password: A-Za-z0-9./
    QString itoa64 = QStringLiteral("./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz");
    QString result;

    while (--size >= 0) {
        result.append(itoa64.at((int)(v & 0x3f)));
        v = v >> 6;
    }

    return result;
}

int byte2unsigned(int byteValue)
{
    int integerToReturn;
    integerToReturn = (int)byteValue & 0xff;
    return integerToReturn;
}

QString qca_md5crypt(const QCA::SecureArray &password, const QCA::SecureArray &salt)
{
    QCA::SecureArray finalState, magic_string = "$1$";

    // The md5crypt algorithm uses two separate hashes
    QCA::Hash hash1(QStringLiteral("md5"));
    QCA::Hash hash2(QStringLiteral("md5"));

    // MD5 Hash #1: pwd, magic string and salt
    hash1.update(password);
    hash1.update(magic_string);
    hash1.update(salt);

    // MD5 Hash #2: password, salt, password
    hash2.update(password);
    hash2.update(salt);
    hash2.update(password);

    finalState = hash2.final();

    // Two sets of transformations based on the length of the password
    for (int i = password.size(); i > 0; i -= 16) {
        // Update hash1 from offset value (i > 16 ? 16 : i)
        hash1.update(finalState.toByteArray().left(i > 16 ? 16 : i));
    }

    // Clear array bits
    finalState.fill(0);

    for (int i = password.size(); i != 0; i = i >> 1) {
        if ((i & 1) != 0) {
            hash1.update(finalState.toByteArray().left(1));
        } else {
            hash1.update(password.toByteArray().left(1));
        }
    }

    finalState = hash1.final();

    // Now build a 1000 entry dictionary...
    for (int i = 0; i < 1000; i++) {
        hash2.clear();

        if ((i & 1) != 0) {
            hash2.update(password);
        } else {
            hash2.update(finalState.toByteArray().left(16));
        }

        if ((i % 3) != 0) {
            hash2.update(salt);
        }

        if ((i % 7) != 0) {
            hash2.update(password);
        }

        if ((i & 1) != 0) {
            hash2.update(finalState.toByteArray().left(16));
        } else {
            hash2.update(password);
        }

        finalState = hash2.final();
    }

    // Create an output string
    // Salt is part of the encoded password ($1$<string>$)
    QString encodedString;

    encodedString.append(QString::fromLatin1(magic_string.toByteArray()));
    encodedString.append(QString::fromLatin1(salt.toByteArray()));
    encodedString.append(QStringLiteral("$"));

    long l;

    l = (byte2unsigned(finalState.toByteArray().at(0)) << 16 | (byte2unsigned(finalState.toByteArray().at(6))) << 8 |
         byte2unsigned(finalState.toByteArray().at(12)));
    encodedString.append(to64(l, 4));

    l = (byte2unsigned(finalState.toByteArray().at(1)) << 16 | (byte2unsigned(finalState.toByteArray().at(7))) << 8 |
         byte2unsigned(finalState.toByteArray().at(13)));
    encodedString.append(to64(l, 4));

    l = (byte2unsigned(finalState.toByteArray().at(2)) << 16 | (byte2unsigned(finalState.toByteArray().at(8))) << 8 |
         byte2unsigned(finalState.toByteArray().at(14)));
    encodedString.append(to64(l, 4));

    l = (byte2unsigned(finalState.toByteArray().at(3)) << 16 | (byte2unsigned(finalState.toByteArray().at(9))) << 8 |
         byte2unsigned(finalState.toByteArray().at(15)));
    encodedString.append(to64(l, 4));

    l = (byte2unsigned(finalState.toByteArray().at(4)) << 16 | (byte2unsigned(finalState.toByteArray().at(10))) << 8 |
         byte2unsigned(finalState.toByteArray().at(5)));
    encodedString.append(to64(l, 4));

    l = byte2unsigned(finalState.toByteArray().at(11));
    encodedString.append(to64(l, 2));

    return encodedString;
}

int main(int argc, char **argv)
{
    // the Initializer object sets things up, and
    // also does cleanup when it goes out of scope
    QCA::Initializer init;

    QCoreApplication app(argc, argv);

    QCA::SecureArray password, salt;

    if (argc < 3) {
        printf("Usage: %s password salt (salt without $1$)\n", argv[0]);
        return 1;
    }

    password.append(argv[1]);

    salt.append(argv[2]);

    // must always check that an algorithm is supported before using it
    if (!QCA::isSupported("md5"))
        printf("MD5 hash not supported!\n");
    else {
        QString result = qca_md5crypt(password, salt);

        printf("md5crypt     [ %s , %s ] = '%s'\n", password.data(), salt.data(), qPrintable(result));

        // this is equivalent if you have GNU libc 2.0
        // printf( "GNU md5crypt [ %s , %s ] = '%s'\n",  password.data(), salt.data(), crypt( password.data(), (
        // "$1$"+salt ).data() ) );
    }

    return 0;
}