예제 #1
0
    def run(self, terms, variables, **kwargs):
        ret = []

        for term in terms:
            relpath, params = _parse_parameters(term)
            path = self._loader.path_dwim(relpath)
            b_path = to_bytes(path, errors='surrogate_or_strict')
            chars = _gen_candidate_chars(params['chars'])

            changed = None
            # make sure only one process finishes all the job first
            first_process, lockfile = _get_lock(b_path)

            content = _read_password_file(b_path)

            if content is None or b_path == to_bytes('/dev/null'):
                plaintext_password = random_password(params['length'], chars,
                                                     params['seed'])
                salt = None
                changed = True
            else:
                plaintext_password, salt = _parse_content(content)

            encrypt = params['encrypt']
            if encrypt and not salt:
                changed = True
                try:
                    salt = random_salt(BaseHash.algorithms[encrypt].salt_size)
                except KeyError:
                    salt = random_salt()

            ident = params['ident']
            if encrypt and not ident:
                changed = True
                try:
                    ident = BaseHash.algorithms[encrypt].implicit_ident
                except KeyError:
                    ident = None

            if changed and b_path != to_bytes('/dev/null'):
                content = _format_content(plaintext_password,
                                          salt,
                                          encrypt=encrypt,
                                          ident=ident)
                _write_password_file(b_path, content)

            if first_process:
                # let other processes continue
                _release_lock(lockfile)

            if encrypt:
                password = do_encrypt(plaintext_password,
                                      encrypt,
                                      salt=salt,
                                      ident=ident)
                ret.append(password)
            else:
                ret.append(plaintext_password)

        return ret
예제 #2
0
    def run(self, terms, variables, **kwargs):
        ret = []

        for term in terms:
            relpath, params = _parse_parameters(term)
            path = self._loader.path_dwim(relpath)
            b_path = to_bytes(path, errors='surrogate_or_strict')
            chars = _gen_candidate_chars(params['chars'])

            changed = None
            # make sure only one process finishes all the job first
            first_process, lockfile = _get_lock(b_path)

            content = _read_password_file(b_path)

            if content is None or b_path == to_bytes('/dev/null'):
                plaintext_password = random_password(params['length'], chars)
                salt = None
                changed = True
            else:
                plaintext_password, salt = _parse_content(content)

            if params['encrypt'] and not salt:
                changed = True
                salt = random_salt()

            if changed and b_path != to_bytes('/dev/null'):
                content = _format_content(plaintext_password,
                                          salt,
                                          encrypt=params['encrypt'])
                _write_password_file(b_path, content)

            if first_process:
                # let other processes continue
                _release_lock(lockfile)

            if params['encrypt']:
                password = do_encrypt(plaintext_password,
                                      params['encrypt'],
                                      salt=salt)
                ret.append(password)
            else:
                ret.append(plaintext_password)

        return ret
예제 #3
0
def test_random_salt():
    res = encrypt.random_salt()
    expected_salt_candidate_chars = u'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789./'
    assert len(res) == 8
    for res_char in res:
        assert res_char in expected_salt_candidate_chars