Statistics
| Branch: | Tag: | Revision:

root / snf-image-helper / hashpwd.py @ 83d0c566

History | View | Annotate | Download (2.4 kB)

1 54080484 Nikos Skalkotos
#!/usr/bin/env python
2 7e5d635b Nikos Skalkotos
3 0925f23a Nikos Skalkotos
# Copyright (C) 2011, 2013 GRNET S.A.
4 7e5d635b Nikos Skalkotos
#
5 7e5d635b Nikos Skalkotos
# This program is free software; you can redistribute it and/or modify
6 7e5d635b Nikos Skalkotos
# it under the terms of the GNU General Public License as published by
7 7e5d635b Nikos Skalkotos
# the Free Software Foundation; either version 2 of the License, or
8 7e5d635b Nikos Skalkotos
# (at your option) any later version.
9 54080484 Nikos Skalkotos
#
10 7e5d635b Nikos Skalkotos
# This program is distributed in the hope that it will be useful, but
11 7e5d635b Nikos Skalkotos
# WITHOUT ANY WARRANTY; without even the implied warranty of
12 7e5d635b Nikos Skalkotos
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 7e5d635b Nikos Skalkotos
# General Public License for more details.
14 54080484 Nikos Skalkotos
#
15 7e5d635b Nikos Skalkotos
# You should have received a copy of the GNU General Public License
16 7e5d635b Nikos Skalkotos
# along with this program; if not, write to the Free Software
17 7e5d635b Nikos Skalkotos
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 7e5d635b Nikos Skalkotos
# 02110-1301, USA.
19 7e5d635b Nikos Skalkotos
20 6a95db10 Vangelis Koukis
"""Generate a hash from a given password
21 54080484 Nikos Skalkotos

22 0925f23a Nikos Skalkotos
This program takes a password as an argument and returns to standard output a
23 0925f23a Nikos Skalkotos
hash followed by a newline.
24 54080484 Nikos Skalkotos

25 54080484 Nikos Skalkotos
"""
26 54080484 Nikos Skalkotos
27 54080484 Nikos Skalkotos
import sys
28 d1628244 Nikos Skalkotos
29 d1628244 Nikos Skalkotos
import passlib.hash
30 54080484 Nikos Skalkotos
31 54080484 Nikos Skalkotos
from string import ascii_letters, digits
32 54080484 Nikos Skalkotos
from random import choice
33 54080484 Nikos Skalkotos
from os.path import basename
34 54080484 Nikos Skalkotos
from optparse import OptionParser
35 54080484 Nikos Skalkotos
36 748282cb Nikos Skalkotos
37 54080484 Nikos Skalkotos
def random_salt(length=8):
38 54080484 Nikos Skalkotos
    pool = ascii_letters + digits + "/" + "."
39 54080484 Nikos Skalkotos
    return ''.join(choice(pool) for i in range(length))
40 54080484 Nikos Skalkotos
41 748282cb Nikos Skalkotos
42 d1628244 Nikos Skalkotos
METHOD = {
43 d1628244 Nikos Skalkotos
#   Name:  (algoritm, options)
44 d1628244 Nikos Skalkotos
    'md5': (passlib.hash.md5_crypt, {}),
45 d1628244 Nikos Skalkotos
    'blowfish': (passlib.hash.bcrypt, {}),
46 d1628244 Nikos Skalkotos
    'sha256': (
47 d1628244 Nikos Skalkotos
        passlib.hash.sha256_crypt,
48 d1628244 Nikos Skalkotos
        {'rounds': 5000, 'implicit_rounds': True, 'salt': random_salt()}),
49 d1628244 Nikos Skalkotos
    'sha512': (
50 d1628244 Nikos Skalkotos
        passlib.hash.sha512_crypt,
51 d1628244 Nikos Skalkotos
        {'rounds': 5000, 'implicit_rounds': True, 'salt': random_salt()}),
52 d1628244 Nikos Skalkotos
    'sha1': (passlib.hash.sha1_crypt, {})
53 d1628244 Nikos Skalkotos
}
54 54080484 Nikos Skalkotos
55 748282cb Nikos Skalkotos
56 54080484 Nikos Skalkotos
def parse_arguments(input_args):
57 54080484 Nikos Skalkotos
    usage = "usage: %prog [-h] [-m encrypt-method] <password>"
58 54080484 Nikos Skalkotos
    parser = OptionParser(usage=usage)
59 f58bbc57 Nikos Skalkotos
    parser.add_option(
60 f58bbc57 Nikos Skalkotos
        "-m", "--encrypt-method", dest="encrypt_method", type='choice',
61 d1628244 Nikos Skalkotos
        default="sha512", choices=METHOD.keys(),
62 f58bbc57 Nikos Skalkotos
        help="encrypt password with ENCRYPT_METHOD [%default] (supported: " +
63 d1628244 Nikos Skalkotos
        ", ".join(METHOD.keys()) + ")"
64 54080484 Nikos Skalkotos
    )
65 54080484 Nikos Skalkotos
66 54080484 Nikos Skalkotos
    (opts, args) = parser.parse_args(input_args)
67 54080484 Nikos Skalkotos
68 54080484 Nikos Skalkotos
    if len(args) != 1:
69 f58bbc57 Nikos Skalkotos
        parser.error('password is missing')
70 54080484 Nikos Skalkotos
71 54080484 Nikos Skalkotos
    return (args[0], opts.encrypt_method)
72 54080484 Nikos Skalkotos
73 54080484 Nikos Skalkotos
74 54080484 Nikos Skalkotos
def main():
75 f58bbc57 Nikos Skalkotos
    (passwd, method) = parse_arguments(sys.argv[1:])
76 b0e6727c Nikos Skalkotos
77 d1628244 Nikos Skalkotos
    algorithm, options = METHOD[method]
78 d1628244 Nikos Skalkotos
    print algorithm.encrypt(passwd, **options)
79 b0e6727c Nikos Skalkotos
80 54080484 Nikos Skalkotos
    return 0
81 54080484 Nikos Skalkotos
82 54080484 Nikos Skalkotos
if __name__ == "__main__":
83 54080484 Nikos Skalkotos
        sys.exit(main())
84 54080484 Nikos Skalkotos
85 54080484 Nikos Skalkotos
# vim: set sta sts=4 shiftwidth=4 sw=4 et ai :