QTfrontend/rndstr.cpp
author displacer
Sun, 20 Aug 2006 17:18:53 +0000
changeset 121 6c7111eddea9
parent 33 d6363050cad0
permissions -rw-r--r--
resize fix
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
     1
/*
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
     2
 * Hedgewars, a worms-like game
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
     3
 * Copyright (c) 2005 Andrey Korotaev <unC0Rr@gmail.com>
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
     4
 *
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
     5
 * Distributed under the terms of the BSD-modified licence:
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
     6
 *
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
     7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
     8
 * of this software and associated documentation files (the "Software"), to deal
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
     9
 * with the Software without restriction, including without limitation the
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    11
 * sell copies of the Software, and to permit persons to whom the Software is
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    12
 * furnished to do so, subject to the following conditions:
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    13
 *
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    14
 * 1. Redistributions of source code must retain the above copyright notice,
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    15
 *    this list of conditions and the following disclaimer.
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    16
 * 2. Redistributions in binary form must reproduce the above copyright notice,
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    17
 *    this list of conditions and the following disclaimer in the documentation
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    18
 *    and/or other materials provided with the distribution.
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    19
 * 3. The name of the author may not be used to endorse or promote products
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    20
 *    derived from this software without specific prior written permission.
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    21
 *
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    22
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    23
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    24
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    25
 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    26
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    28
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    29
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    30
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    31
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    32
 */
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    33
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    34
#include <QDateTime>
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    35
#include "rndstr.h"
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    36
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    37
const char * letters = "qwertyuiopasdfghjklzxcvbnm" \
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    38
                       "QWERTYUIOPASDFGHJKLZXCVBNM" \
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    39
                       "0123456789";
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    40
const quint8 letterscnt = 62;
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    41
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    42
const char * upd = "/hw!/";
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    43
const quint8 updcnt = 5;
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    44
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    45
RNDStr::RNDStr()
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    46
{
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    47
	SHA1Init(&ctx);
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    48
	QDateTime now = QDateTime::currentDateTime();
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    49
	QDateTime zero;
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    50
	int secs = now.secsTo(zero);
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    51
	SHA1Update(&ctx, (quint8 *)&secs, sizeof(int));
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    52
}
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    53
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    54
void RNDStr::GenRNDStr(QString & str, quint32 len)
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    55
{
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    56
	str = "";
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    57
	sha1_ctxt tmpctx;
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    58
	sha1_digest digest;
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    59
	for(quint32 i = 0; i < len; i++)
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    60
	{
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    61
		SHA1Update(&ctx, (quint8 *)upd, updcnt);
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    62
		qMemCopy(&tmpctx, &ctx, sizeof(sha1_ctxt));
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    63
		SHA1Final(digest, &tmpctx);
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    64
		int index = (digest[3] + digest[11] + digest[17]) % letterscnt;
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    65
		str += letters[index];
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    66
	}
d6363050cad0 fix to compile on Linux
unc0rr
parents: 21
diff changeset
    67
}