<commit_before>#
# crypto.py
#
# Copyright (C) 2009  Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Author(s): <NAME> <<EMAIL>>
#            <NAME> <<EMAIL>>
#

import random
from pycryptsetup import CryptSetup
from six.moves import xrange

from ..errors import CryptoError
from ..size import Size

LUKS_METADATA_SIZE = Size("2 MiB")

# Keep the character set size a power of two to make sure all characters are
# equally likely
GENERATED_PASSPHRASE_CHARSET = ("0123456789"
                                "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                                "abcdefghijklmnopqrstuvwxyz"
                                "./")
# 20 chars * 6 bits per char = 120 "bits of security"
GENERATED_PASSPHRASE_LENGTH = 20

def generateBackupPassphrase():
    raw = ""
    for i in xrange(GENERATED_PASSPHRASE_LENGTH):
        raw += random.choice(GENERATED_PASSPHRASE_CHARSET)

    # Make the result easier to read
    parts = []
    for i in xrange(0, len(raw), 5):
        parts.append(raw[i : i + 5])
    return "-".join(parts)

yesDialog = lambda q: True
logFunc = lambda p, t: None
passwordDialog = lambda t: None

def is_luks(device):
    cs = CryptSetup(device=device, yesDialog=yesDialog, logFunc=logFunc, passwordDialog=passwordDialog)
    return cs.isLuks()

def luks_uuid(device):
    cs = CryptSetup(device=device, yesDialog=yesDialog, logFunc=logFunc, passwordDialog=passwordDialog)
    return cs.luksUUID()

def luks_status(name):
    """True means active, False means inactive (or non-existent)"""
    cs = CryptSetup(name=name, yesDialog=yesDialog, logFunc=logFunc, passwordDialog=passwordDialog)
    return cs.status()

def luks_format(device,
                passphrase=None,
                cipher=None, key_size=None, key_file=None):
    # pylint: disable=unused-argument
    if not passphrase:
        raise ValueError("luks_format requires passphrase")
<commit_msg>Use range instead of xrange in generateBackupPassphrase()<commit_after>#
# crypto.py
#
# Copyright (C) 2009  Red Hat, Inc.  All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Author(s): <NAME> <<EMAIL>>
#            <NAME> <<EMAIL>>
#

import random
from pycryptsetup import CryptSetup

from ..errors import CryptoError
from ..size import Size

LUKS_METADATA_SIZE = Size("2 MiB")

# Keep the character set size a power of two to make sure all characters are
# equally likely
GENERATED_PASSPHRASE_CHARSET = ("0123456789"
                                "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                                "abcdefghijklmnopqrstuvwxyz"
                                "./")
# 20 chars * 6 bits per char = 120 "bits of security"
GENERATED_PASSPHRASE_LENGTH = 20

def generateBackupPassphrase():
    raw = [random.choice(GENERATED_PASSPHRASE_CHARSET) for _ in range(GENERATED_PASSPHRASE_LENGTH)]

    # Insert a '-' after every five char chunk for easier reading
    parts = []
    for i in range(0, GENERATED_PASSPHRASE_LENGTH, 5):
        parts.append(''.join(raw[i : i + 5]))
    return "-".join(parts)

yesDialog = lambda q: True
logFunc = lambda p, t: None
passwordDialog = lambda t: None

def is_luks(device):
    cs = CryptSetup(device=device, yesDialog=yesDialog, logFunc=logFunc, passwordDialog=passwordDialog)
    return cs.isLuks()

def luks_uuid(device):
    cs = CryptSetup(device=device, yesDialog=yesDialog, logFunc=logFunc, passwordDialog=passwordDialog)
    return cs.luksUUID()

def luks_status(name):
    """True means active, False means inactive (or non-existent)"""
    cs = CryptSetup(name=name, yesDialog=yesDialog, logFunc=logFunc, passwordDialog=passwordDialog)
    return cs.status()

def luks_format(device,
                passphrase=None,
                cipher=None, key_size=None, key_file=None):
    # pylint: disable=unused-argument
    if not passphrase:
        raise ValueError("luks_format requires passphrase")

{'fraction_non_alphanumeric': 0.08820633983067533, 'fraction_numerical': 0.010632014176018901, 'mean_word_length': 3.4639718804920916, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2855123', 'n_tokens_mistral': 1557, 'n_tokens_neox': 1486, 'n_words': 522}