Completed
Push — master ( 7beec5...72ed44 )
by Marek
17s queued 14s
created

ige.account.Account.hashPassword()   A

Complexity

Conditions 3

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 5
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
#
2
#  Copyright 2001 - 2018 Ludek Smid [http://www.ospace.net/]
3
#
4
#  This file is part of Outer Space.
5
#
6
#  Outer Space is free software; you can redistribute it and/or modify
7
#  it under the terms of the GNU General Public License as published by
8
#  the Free Software Foundation; either version 2 of the License, or
9
#  (at your option) any later version.
10
#
11
#  Outer Space is distributed in the hope that it will be useful,
12
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
13
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
#  GNU General Public License for more details.
15
#
16
#  You should have received a copy of the GNU General Public License
17
#  along with Outer Space; if not, write to the Free Software
18
#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
#
20
21
import hashlib
22
import random
23
import time
24
25
class Account(object):
26
27
    def __init__(self, login, nick, email, passwd, passwdHash = "sha256"):
28
        self._passwdSalt = hashlib.sha1(str(random.randrange(0, 1e10))).hexdigest()
29
        # credentials
30
        self.login = login
31
        self.nick = nick
32
        self.email = email
33
        self.passwdHash = passwdHash
34
        self.passwd = self.hashPassword(passwd)
35
        # account options
36
        self.lastLogin = 0
37
        self.blockedUntil = -1 # -1 for not blocked, > 0 for blocked
38
        self.blocked = 0 # 1 for blocked account
39
        self.confToken = hashlib.md5('%s%s%d' % (login, email, time.time())).hexdigest() # when None, it's confirmed TODO make it work
40
        self.hostIDs = {} # hostids and times
41
        self.isAI = False
42
43
    def addHostID(self, hostID):
44
        if hostID:
45
            self.hostIDs[hostID] = time.time()
46
47
    def hashPassword(self, plainPassword):
48
        if self.passwdHash is None:
49
            return plainPassword
50
        elif self.passwdHash == "sha256":
51
            return hashlib.sha256(self._passwdSalt + plainPassword).hexdigest()
52
53
class AIAccount(Account):
54
55
    def __init__(self, login, nick, aiType):
56
        password = hashlib.sha1(str(random.randrange(0, 1e10))).hexdigest()
57
        Account.__init__(self, login, nick, None, password, passwdHash = None)
58
59
        self.isAI = True
60
        self.aiType = aiType
61
        self.galaxyNames = []
62