rm unwanted files in git
This commit is contained in:
parent
2656c7fc02
commit
7360923b2d
992 changed files with 0 additions and 246162 deletions
|
@ -1,54 +0,0 @@
|
|||
action: GLOB, msg: packaging
|
||||
cwd: /home/amnesia/dev/mega.py
|
||||
cmd: /home/amnesia/.pyenv/versions/3.7.4/envs/megapy/bin/python3.7 setup.py sdist --formats=zip --dist-dir /home/amnesia/dev/mega.py/.tox/dist
|
||||
running sdist
|
||||
running egg_info
|
||||
writing src/mega.py.egg-info/PKG-INFO
|
||||
writing dependency_links to src/mega.py.egg-info/dependency_links.txt
|
||||
writing requirements to src/mega.py.egg-info/requires.txt
|
||||
writing top-level names to src/mega.py.egg-info/top_level.txt
|
||||
reading manifest file 'src/mega.py.egg-info/SOURCES.txt'
|
||||
writing manifest file 'src/mega.py.egg-info/SOURCES.txt'
|
||||
running check
|
||||
warning: check: missing required meta-data: url
|
||||
|
||||
creating mega.py-0.9.17
|
||||
creating mega.py-0.9.17/src
|
||||
creating mega.py-0.9.17/src/mega
|
||||
creating mega.py-0.9.17/src/mega.py.egg-info
|
||||
copying files to mega.py-0.9.17...
|
||||
copying README.rst -> mega.py-0.9.17
|
||||
copying setup.cfg -> mega.py-0.9.17
|
||||
copying setup.py -> mega.py-0.9.17
|
||||
copying src/mega/__init__.py -> mega.py-0.9.17/src/mega
|
||||
copying src/mega/crypto.py -> mega.py-0.9.17/src/mega
|
||||
copying src/mega/errors.py -> mega.py-0.9.17/src/mega
|
||||
copying src/mega/mega.py -> mega.py-0.9.17/src/mega
|
||||
copying src/mega.py.egg-info/PKG-INFO -> mega.py-0.9.17/src/mega.py.egg-info
|
||||
copying src/mega.py.egg-info/SOURCES.txt -> mega.py-0.9.17/src/mega.py.egg-info
|
||||
copying src/mega.py.egg-info/dependency_links.txt -> mega.py-0.9.17/src/mega.py.egg-info
|
||||
copying src/mega.py.egg-info/not-zip-safe -> mega.py-0.9.17/src/mega.py.egg-info
|
||||
copying src/mega.py.egg-info/requires.txt -> mega.py-0.9.17/src/mega.py.egg-info
|
||||
copying src/mega.py.egg-info/top_level.txt -> mega.py-0.9.17/src/mega.py.egg-info
|
||||
Writing mega.py-0.9.17/setup.cfg
|
||||
creating /home/amnesia/dev/mega.py/.tox/dist
|
||||
creating '/home/amnesia/dev/mega.py/.tox/dist/mega.py-0.9.17.zip' and adding 'mega.py-0.9.17' to it
|
||||
adding 'mega.py-0.9.17'
|
||||
adding 'mega.py-0.9.17/src'
|
||||
adding 'mega.py-0.9.17/README.rst'
|
||||
adding 'mega.py-0.9.17/setup.py'
|
||||
adding 'mega.py-0.9.17/PKG-INFO'
|
||||
adding 'mega.py-0.9.17/setup.cfg'
|
||||
adding 'mega.py-0.9.17/src/mega.py.egg-info'
|
||||
adding 'mega.py-0.9.17/src/mega'
|
||||
adding 'mega.py-0.9.17/src/mega.py.egg-info/PKG-INFO'
|
||||
adding 'mega.py-0.9.17/src/mega.py.egg-info/top_level.txt'
|
||||
adding 'mega.py-0.9.17/src/mega.py.egg-info/dependency_links.txt'
|
||||
adding 'mega.py-0.9.17/src/mega.py.egg-info/not-zip-safe'
|
||||
adding 'mega.py-0.9.17/src/mega.py.egg-info/requires.txt'
|
||||
adding 'mega.py-0.9.17/src/mega.py.egg-info/SOURCES.txt'
|
||||
adding 'mega.py-0.9.17/src/mega/errors.py'
|
||||
adding 'mega.py-0.9.17/src/mega/mega.py'
|
||||
adding 'mega.py-0.9.17/src/mega/crypto.py'
|
||||
adding 'mega.py-0.9.17/src/mega/__init__.py'
|
||||
removing 'mega.py-0.9.17' (and everything under it)
|
|
@ -1,84 +0,0 @@
|
|||
# This file must be used with "source bin/activate" *from bash*
|
||||
# you cannot run it directly
|
||||
|
||||
|
||||
if [ "${BASH_SOURCE-}" = "$0" ]; then
|
||||
echo "You must source this script: \$ source $0" >&2
|
||||
exit 33
|
||||
fi
|
||||
|
||||
deactivate () {
|
||||
unset -f pydoc >/dev/null 2>&1
|
||||
|
||||
# reset old environment variables
|
||||
# ! [ -z ${VAR+_} ] returns true if VAR is declared at all
|
||||
if ! [ -z "${_OLD_VIRTUAL_PATH:+_}" ] ; then
|
||||
PATH="$_OLD_VIRTUAL_PATH"
|
||||
export PATH
|
||||
unset _OLD_VIRTUAL_PATH
|
||||
fi
|
||||
if ! [ -z "${_OLD_VIRTUAL_PYTHONHOME+_}" ] ; then
|
||||
PYTHONHOME="$_OLD_VIRTUAL_PYTHONHOME"
|
||||
export PYTHONHOME
|
||||
unset _OLD_VIRTUAL_PYTHONHOME
|
||||
fi
|
||||
|
||||
# This should detect bash and zsh, which have a hash command that must
|
||||
# be called to get it to forget past commands. Without forgetting
|
||||
# past commands the $PATH changes we made may not be respected
|
||||
if [ -n "${BASH-}" ] || [ -n "${ZSH_VERSION-}" ] ; then
|
||||
hash -r 2>/dev/null
|
||||
fi
|
||||
|
||||
if ! [ -z "${_OLD_VIRTUAL_PS1+_}" ] ; then
|
||||
PS1="$_OLD_VIRTUAL_PS1"
|
||||
export PS1
|
||||
unset _OLD_VIRTUAL_PS1
|
||||
fi
|
||||
|
||||
unset VIRTUAL_ENV
|
||||
if [ ! "${1-}" = "nondestructive" ] ; then
|
||||
# Self destruct!
|
||||
unset -f deactivate
|
||||
fi
|
||||
}
|
||||
|
||||
# unset irrelevant variables
|
||||
deactivate nondestructive
|
||||
|
||||
VIRTUAL_ENV="/home/amnesia/dev/mega.py/.tox/py37-normal"
|
||||
export VIRTUAL_ENV
|
||||
|
||||
_OLD_VIRTUAL_PATH="$PATH"
|
||||
PATH="$VIRTUAL_ENV/bin:$PATH"
|
||||
export PATH
|
||||
|
||||
# unset PYTHONHOME if set
|
||||
if ! [ -z "${PYTHONHOME+_}" ] ; then
|
||||
_OLD_VIRTUAL_PYTHONHOME="$PYTHONHOME"
|
||||
unset PYTHONHOME
|
||||
fi
|
||||
|
||||
if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT-}" ] ; then
|
||||
_OLD_VIRTUAL_PS1="${PS1-}"
|
||||
if [ "x" != x ] ; then
|
||||
PS1="${PS1-}"
|
||||
else
|
||||
PS1="(`basename \"$VIRTUAL_ENV\"`) ${PS1-}"
|
||||
fi
|
||||
export PS1
|
||||
fi
|
||||
|
||||
# Make sure to unalias pydoc if it's already there
|
||||
alias pydoc 2>/dev/null >/dev/null && unalias pydoc || true
|
||||
|
||||
pydoc () {
|
||||
python -m pydoc "$@"
|
||||
}
|
||||
|
||||
# This should detect bash and zsh, which have a hash command that must
|
||||
# be called to get it to forget past commands. Without forgetting
|
||||
# past commands the $PATH changes we made may not be respected
|
||||
if [ -n "${BASH-}" ] || [ -n "${ZSH_VERSION-}" ] ; then
|
||||
hash -r 2>/dev/null
|
||||
fi
|
|
@ -1,55 +0,0 @@
|
|||
# This file must be used with "source bin/activate.csh" *from csh*.
|
||||
# You cannot run it directly.
|
||||
# Created by Davide Di Blasi <davidedb@gmail.com>.
|
||||
|
||||
set newline='\
|
||||
'
|
||||
|
||||
alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH:q" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT:q" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; test "\!:*" != "nondestructive" && unalias deactivate && unalias pydoc'
|
||||
|
||||
# Unset irrelevant variables.
|
||||
deactivate nondestructive
|
||||
|
||||
setenv VIRTUAL_ENV "/home/amnesia/dev/mega.py/.tox/py37-normal"
|
||||
|
||||
set _OLD_VIRTUAL_PATH="$PATH:q"
|
||||
setenv PATH "$VIRTUAL_ENV:q/bin:$PATH:q"
|
||||
|
||||
|
||||
|
||||
if ("" != "") then
|
||||
set env_name = ""
|
||||
else
|
||||
set env_name = '('"$VIRTUAL_ENV:t:q"') '
|
||||
endif
|
||||
|
||||
if ( $?VIRTUAL_ENV_DISABLE_PROMPT ) then
|
||||
if ( $VIRTUAL_ENV_DISABLE_PROMPT == "" ) then
|
||||
set do_prompt = "1"
|
||||
else
|
||||
set do_prompt = "0"
|
||||
endif
|
||||
else
|
||||
set do_prompt = "1"
|
||||
endif
|
||||
|
||||
if ( $do_prompt == "1" ) then
|
||||
# Could be in a non-interactive environment,
|
||||
# in which case, $prompt is undefined and we wouldn't
|
||||
# care about the prompt anyway.
|
||||
if ( $?prompt ) then
|
||||
set _OLD_VIRTUAL_PROMPT="$prompt:q"
|
||||
if ( "$prompt:q" =~ *"$newline:q"* ) then
|
||||
:
|
||||
else
|
||||
set prompt = "$env_name:q$prompt:q"
|
||||
endif
|
||||
endif
|
||||
endif
|
||||
|
||||
unset env_name
|
||||
unset do_prompt
|
||||
|
||||
alias pydoc python -m pydoc
|
||||
|
||||
rehash
|
|
@ -1,102 +0,0 @@
|
|||
# This file must be used using `source bin/activate.fish` *within a running fish ( http://fishshell.com ) session*.
|
||||
# Do not run it directly.
|
||||
|
||||
function _bashify_path -d "Converts a fish path to something bash can recognize"
|
||||
set fishy_path $argv
|
||||
set bashy_path $fishy_path[1]
|
||||
for path_part in $fishy_path[2..-1]
|
||||
set bashy_path "$bashy_path:$path_part"
|
||||
end
|
||||
echo $bashy_path
|
||||
end
|
||||
|
||||
function _fishify_path -d "Converts a bash path to something fish can recognize"
|
||||
echo $argv | tr ':' '\n'
|
||||
end
|
||||
|
||||
function deactivate -d 'Exit virtualenv mode and return to the normal environment.'
|
||||
# reset old environment variables
|
||||
if test -n "$_OLD_VIRTUAL_PATH"
|
||||
# https://github.com/fish-shell/fish-shell/issues/436 altered PATH handling
|
||||
if test (echo $FISH_VERSION | tr "." "\n")[1] -lt 3
|
||||
set -gx PATH (_fishify_path $_OLD_VIRTUAL_PATH)
|
||||
else
|
||||
set -gx PATH $_OLD_VIRTUAL_PATH
|
||||
end
|
||||
set -e _OLD_VIRTUAL_PATH
|
||||
end
|
||||
|
||||
if test -n "$_OLD_VIRTUAL_PYTHONHOME"
|
||||
set -gx PYTHONHOME $_OLD_VIRTUAL_PYTHONHOME
|
||||
set -e _OLD_VIRTUAL_PYTHONHOME
|
||||
end
|
||||
|
||||
if test -n "$_OLD_FISH_PROMPT_OVERRIDE"
|
||||
and functions -q _old_fish_prompt
|
||||
# Set an empty local `$fish_function_path` to allow the removal of `fish_prompt` using `functions -e`.
|
||||
set -l fish_function_path
|
||||
|
||||
# Erase virtualenv's `fish_prompt` and restore the original.
|
||||
functions -e fish_prompt
|
||||
functions -c _old_fish_prompt fish_prompt
|
||||
functions -e _old_fish_prompt
|
||||
set -e _OLD_FISH_PROMPT_OVERRIDE
|
||||
end
|
||||
|
||||
set -e VIRTUAL_ENV
|
||||
|
||||
if test "$argv[1]" != 'nondestructive'
|
||||
# Self-destruct!
|
||||
functions -e pydoc
|
||||
functions -e deactivate
|
||||
functions -e _bashify_path
|
||||
functions -e _fishify_path
|
||||
end
|
||||
end
|
||||
|
||||
# Unset irrelevant variables.
|
||||
deactivate nondestructive
|
||||
|
||||
set -gx VIRTUAL_ENV "/home/amnesia/dev/mega.py/.tox/py37-normal"
|
||||
|
||||
# https://github.com/fish-shell/fish-shell/issues/436 altered PATH handling
|
||||
if test (echo $FISH_VERSION | tr "." "\n")[1] -lt 3
|
||||
set -gx _OLD_VIRTUAL_PATH (_bashify_path $PATH)
|
||||
else
|
||||
set -gx _OLD_VIRTUAL_PATH $PATH
|
||||
end
|
||||
set -gx PATH "$VIRTUAL_ENV/bin" $PATH
|
||||
|
||||
# Unset `$PYTHONHOME` if set.
|
||||
if set -q PYTHONHOME
|
||||
set -gx _OLD_VIRTUAL_PYTHONHOME $PYTHONHOME
|
||||
set -e PYTHONHOME
|
||||
end
|
||||
|
||||
function pydoc
|
||||
python -m pydoc $argv
|
||||
end
|
||||
|
||||
if test -z "$VIRTUAL_ENV_DISABLE_PROMPT"
|
||||
# Copy the current `fish_prompt` function as `_old_fish_prompt`.
|
||||
functions -c fish_prompt _old_fish_prompt
|
||||
|
||||
function fish_prompt
|
||||
# Save the current $status, for fish_prompts that display it.
|
||||
set -l old_status $status
|
||||
|
||||
# Prompt override provided?
|
||||
# If not, just prepend the environment name.
|
||||
if test -n ""
|
||||
printf '%s%s' "" (set_color normal)
|
||||
else
|
||||
printf '%s(%s) ' (set_color normal) (basename "$VIRTUAL_ENV")
|
||||
end
|
||||
|
||||
# Restore the original $status
|
||||
echo "exit $old_status" | source
|
||||
_old_fish_prompt
|
||||
end
|
||||
|
||||
set -gx _OLD_FISH_PROMPT_OVERRIDE "$VIRTUAL_ENV"
|
||||
end
|
|
@ -1,60 +0,0 @@
|
|||
$script:THIS_PATH = $myinvocation.mycommand.path
|
||||
$script:BASE_DIR = Split-Path (Resolve-Path "$THIS_PATH/..") -Parent
|
||||
|
||||
function global:deactivate([switch] $NonDestructive) {
|
||||
if (Test-Path variable:_OLD_VIRTUAL_PATH) {
|
||||
$env:PATH = $variable:_OLD_VIRTUAL_PATH
|
||||
Remove-Variable "_OLD_VIRTUAL_PATH" -Scope global
|
||||
}
|
||||
|
||||
if (Test-Path function:_old_virtual_prompt) {
|
||||
$function:prompt = $function:_old_virtual_prompt
|
||||
Remove-Item function:\_old_virtual_prompt
|
||||
}
|
||||
|
||||
if ($env:VIRTUAL_ENV) {
|
||||
Remove-Item env:VIRTUAL_ENV -ErrorAction SilentlyContinue
|
||||
}
|
||||
|
||||
if (!$NonDestructive) {
|
||||
# Self destruct!
|
||||
Remove-Item function:deactivate
|
||||
Remove-Item function:pydoc
|
||||
}
|
||||
}
|
||||
|
||||
function global:pydoc {
|
||||
python -m pydoc $args
|
||||
}
|
||||
|
||||
# unset irrelevant variables
|
||||
deactivate -nondestructive
|
||||
|
||||
$VIRTUAL_ENV = $BASE_DIR
|
||||
$env:VIRTUAL_ENV = $VIRTUAL_ENV
|
||||
|
||||
New-Variable -Scope global -Name _OLD_VIRTUAL_PATH -Value $env:PATH
|
||||
|
||||
$env:PATH = "$env:VIRTUAL_ENV/bin:" + $env:PATH
|
||||
if (!$env:VIRTUAL_ENV_DISABLE_PROMPT) {
|
||||
function global:_old_virtual_prompt {
|
||||
""
|
||||
}
|
||||
$function:_old_virtual_prompt = $function:prompt
|
||||
|
||||
if ("" -ne "") {
|
||||
function global:prompt {
|
||||
# Add the custom prefix to the existing prompt
|
||||
$previous_prompt_value = & $function:_old_virtual_prompt
|
||||
("" + $previous_prompt_value)
|
||||
}
|
||||
}
|
||||
else {
|
||||
function global:prompt {
|
||||
# Add a prefix to the current prompt, but don't discard it.
|
||||
$previous_prompt_value = & $function:_old_virtual_prompt
|
||||
$new_prompt_value = "($( Split-Path $env:VIRTUAL_ENV -Leaf )) "
|
||||
($new_prompt_value + $previous_prompt_value)
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
"""Xonsh activate script for virtualenv"""
|
||||
from xonsh.tools import get_sep as _get_sep
|
||||
|
||||
def _deactivate(args):
|
||||
if "pydoc" in aliases:
|
||||
del aliases["pydoc"]
|
||||
|
||||
if ${...}.get("_OLD_VIRTUAL_PATH", ""):
|
||||
$PATH = $_OLD_VIRTUAL_PATH
|
||||
del $_OLD_VIRTUAL_PATH
|
||||
|
||||
if ${...}.get("_OLD_VIRTUAL_PYTHONHOME", ""):
|
||||
$PYTHONHOME = $_OLD_VIRTUAL_PYTHONHOME
|
||||
del $_OLD_VIRTUAL_PYTHONHOME
|
||||
|
||||
if "VIRTUAL_ENV" in ${...}:
|
||||
del $VIRTUAL_ENV
|
||||
|
||||
if "VIRTUAL_ENV_PROMPT" in ${...}:
|
||||
del $VIRTUAL_ENV_PROMPT
|
||||
|
||||
if "nondestructive" not in args:
|
||||
# Self destruct!
|
||||
del aliases["deactivate"]
|
||||
|
||||
|
||||
# unset irrelevant variables
|
||||
_deactivate(["nondestructive"])
|
||||
aliases["deactivate"] = _deactivate
|
||||
|
||||
$VIRTUAL_ENV = r"/home/amnesia/dev/mega.py/.tox/py37-normal"
|
||||
|
||||
$_OLD_VIRTUAL_PATH = $PATH
|
||||
$PATH = $PATH[:]
|
||||
$PATH.add($VIRTUAL_ENV + _get_sep() + "bin", front=True, replace=True)
|
||||
|
||||
if ${...}.get("PYTHONHOME", ""):
|
||||
# unset PYTHONHOME if set
|
||||
$_OLD_VIRTUAL_PYTHONHOME = $PYTHONHOME
|
||||
del $PYTHONHOME
|
||||
|
||||
$VIRTUAL_ENV_PROMPT = ""
|
||||
if not $VIRTUAL_ENV_PROMPT:
|
||||
del $VIRTUAL_ENV_PROMPT
|
||||
|
||||
aliases["pydoc"] = ["python", "-m", "pydoc"]
|
|
@ -1,46 +0,0 @@
|
|||
"""Activate virtualenv for current interpreter:
|
||||
|
||||
Use exec(open(this_file).read(), {'__file__': this_file}).
|
||||
|
||||
This can be used when you must use an existing Python interpreter, not the virtualenv bin/python.
|
||||
"""
|
||||
import os
|
||||
import site
|
||||
import sys
|
||||
|
||||
try:
|
||||
__file__
|
||||
except NameError:
|
||||
raise AssertionError("You must use exec(open(this_file).read(), {'__file__': this_file}))")
|
||||
|
||||
# prepend bin to PATH (this file is inside the bin directory)
|
||||
bin_dir = os.path.dirname(os.path.abspath(__file__))
|
||||
os.environ["PATH"] = os.pathsep.join([bin_dir] + os.environ.get("PATH", "").split(os.pathsep))
|
||||
|
||||
base = os.path.dirname(bin_dir)
|
||||
|
||||
# virtual env is right above bin directory
|
||||
os.environ["VIRTUAL_ENV"] = base
|
||||
|
||||
# add the virtual environments site-package to the host python import mechanism
|
||||
IS_PYPY = hasattr(sys, "pypy_version_info")
|
||||
IS_JYTHON = sys.platform.startswith("java")
|
||||
if IS_JYTHON:
|
||||
site_packages = os.path.join(base, "Lib", "site-packages")
|
||||
elif IS_PYPY:
|
||||
site_packages = os.path.join(base, "site-packages")
|
||||
else:
|
||||
IS_WIN = sys.platform == "win32"
|
||||
if IS_WIN:
|
||||
site_packages = os.path.join(base, "Lib", "site-packages")
|
||||
else:
|
||||
site_packages = os.path.join(base, "lib", "python{}.{}".format(*sys.version_info), "site-packages")
|
||||
|
||||
prev = set(sys.path)
|
||||
site.addsitedir(site_packages)
|
||||
sys.real_prefix = sys.prefix
|
||||
sys.prefix = base
|
||||
|
||||
# Move the added items to the front of the path, in place
|
||||
new = list(sys.path)
|
||||
sys.path[:] = [i for i in new if i not in prev] + [i for i in new if i in prev]
|
|
@ -1,8 +0,0 @@
|
|||
#!/home/amnesia/dev/mega.py/.tox/py37-normal/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
from chardet.cli.chardetect import main
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
|
@ -1,10 +0,0 @@
|
|||
#!/home/amnesia/dev/mega.py/.tox/py37-normal/bin/python3.7
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
|
||||
from setuptools.command.easy_install import main
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
|
@ -1,10 +0,0 @@
|
|||
#!/home/amnesia/dev/mega.py/.tox/py37-normal/bin/python3.7
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
|
||||
from setuptools.command.easy_install import main
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
|
@ -1,10 +0,0 @@
|
|||
#!/home/amnesia/dev/mega.py/.tox/py37-normal/bin/python3.7
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
|
||||
from pip._internal.main import main
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
|
@ -1,10 +0,0 @@
|
|||
#!/home/amnesia/dev/mega.py/.tox/py37-normal/bin/python3.7
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
|
||||
from pip._internal.main import main
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
|
@ -1,10 +0,0 @@
|
|||
#!/home/amnesia/dev/mega.py/.tox/py37-normal/bin/python3.7
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
|
||||
from pip._internal.main import main
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
|
@ -1 +0,0 @@
|
|||
python3.7
|
|
@ -1,78 +0,0 @@
|
|||
#!/home/amnesia/dev/mega.py/.tox/py37-normal/bin/python
|
||||
|
||||
import sys
|
||||
import getopt
|
||||
import sysconfig
|
||||
|
||||
valid_opts = ['prefix', 'exec-prefix', 'includes', 'libs', 'cflags',
|
||||
'ldflags', 'help']
|
||||
|
||||
if sys.version_info >= (3, 2):
|
||||
valid_opts.insert(-1, 'extension-suffix')
|
||||
valid_opts.append('abiflags')
|
||||
if sys.version_info >= (3, 3):
|
||||
valid_opts.append('configdir')
|
||||
|
||||
|
||||
def exit_with_usage(code=1):
|
||||
sys.stderr.write("Usage: {0} [{1}]\n".format(
|
||||
sys.argv[0], '|'.join('--'+opt for opt in valid_opts)))
|
||||
sys.exit(code)
|
||||
|
||||
try:
|
||||
opts, args = getopt.getopt(sys.argv[1:], '', valid_opts)
|
||||
except getopt.error:
|
||||
exit_with_usage()
|
||||
|
||||
if not opts:
|
||||
exit_with_usage()
|
||||
|
||||
pyver = sysconfig.get_config_var('VERSION')
|
||||
getvar = sysconfig.get_config_var
|
||||
|
||||
opt_flags = [flag for (flag, val) in opts]
|
||||
|
||||
if '--help' in opt_flags:
|
||||
exit_with_usage(code=0)
|
||||
|
||||
for opt in opt_flags:
|
||||
if opt == '--prefix':
|
||||
print(sysconfig.get_config_var('prefix'))
|
||||
|
||||
elif opt == '--exec-prefix':
|
||||
print(sysconfig.get_config_var('exec_prefix'))
|
||||
|
||||
elif opt in ('--includes', '--cflags'):
|
||||
flags = ['-I' + sysconfig.get_path('include'),
|
||||
'-I' + sysconfig.get_path('platinclude')]
|
||||
if opt == '--cflags':
|
||||
flags.extend(getvar('CFLAGS').split())
|
||||
print(' '.join(flags))
|
||||
|
||||
elif opt in ('--libs', '--ldflags'):
|
||||
abiflags = getattr(sys, 'abiflags', '')
|
||||
libs = ['-lpython' + pyver + abiflags]
|
||||
libs += getvar('LIBS').split()
|
||||
libs += getvar('SYSLIBS').split()
|
||||
# add the prefix/lib/pythonX.Y/config dir, but only if there is no
|
||||
# shared library in prefix/lib/.
|
||||
if opt == '--ldflags':
|
||||
if not getvar('Py_ENABLE_SHARED'):
|
||||
libs.insert(0, '-L' + getvar('LIBPL'))
|
||||
if not getvar('PYTHONFRAMEWORK'):
|
||||
libs.extend(getvar('LINKFORSHARED').split())
|
||||
print(' '.join(libs))
|
||||
|
||||
elif opt == '--extension-suffix':
|
||||
ext_suffix = sysconfig.get_config_var('EXT_SUFFIX')
|
||||
if ext_suffix is None:
|
||||
ext_suffix = sysconfig.get_config_var('SO')
|
||||
print(ext_suffix)
|
||||
|
||||
elif opt == '--abiflags':
|
||||
if not getattr(sys, 'abiflags', None):
|
||||
exit_with_usage()
|
||||
print(sys.abiflags)
|
||||
|
||||
elif opt == '--configdir':
|
||||
print(sysconfig.get_config_var('LIBPL'))
|
|
@ -1 +0,0 @@
|
|||
python3.7
|
Binary file not shown.
|
@ -1,10 +0,0 @@
|
|||
#!/home/amnesia/dev/mega.py/.tox/py37-normal/bin/python3.7
|
||||
# -*- coding: utf-8 -*-
|
||||
import re
|
||||
import sys
|
||||
|
||||
from wheel.cli import main
|
||||
|
||||
if __name__ == '__main__':
|
||||
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
|
||||
sys.exit(main())
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/include/python3.7m
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/LICENSE.txt
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/__future__.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/_bootlocale.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/_collections_abc.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/_dummy_thread.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/_weakrefset.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/abc.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/base64.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/bisect.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/codecs.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/collections
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/config-3.7m-x86_64-linux-gnu
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/copy.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/copyreg.py
|
|
@ -1,134 +0,0 @@
|
|||
import os
|
||||
import sys
|
||||
import warnings
|
||||
|
||||
# opcode is not a virtualenv module, so we can use it to find the stdlib
|
||||
# Important! To work on pypy, this must be a module that resides in the
|
||||
# lib-python/modified-x.y.z directory
|
||||
import opcode
|
||||
|
||||
dirname = os.path.dirname
|
||||
|
||||
distutils_path = os.path.join(os.path.dirname(opcode.__file__), "distutils")
|
||||
if os.path.normpath(distutils_path) == os.path.dirname(os.path.normpath(__file__)):
|
||||
warnings.warn("The virtualenv distutils package at %s appears to be in the same location as the system distutils?")
|
||||
else:
|
||||
__path__.insert(0, distutils_path) # noqa: F821
|
||||
if sys.version_info < (3, 4):
|
||||
import imp
|
||||
|
||||
real_distutils = imp.load_module("_virtualenv_distutils", None, distutils_path, ("", "", imp.PKG_DIRECTORY))
|
||||
else:
|
||||
import importlib.machinery
|
||||
|
||||
distutils_path = os.path.join(distutils_path, "__init__.py")
|
||||
loader = importlib.machinery.SourceFileLoader("_virtualenv_distutils", distutils_path)
|
||||
if sys.version_info < (3, 5):
|
||||
import types
|
||||
|
||||
real_distutils = types.ModuleType(loader.name)
|
||||
else:
|
||||
import importlib.util
|
||||
|
||||
spec = importlib.util.spec_from_loader(loader.name, loader)
|
||||
real_distutils = importlib.util.module_from_spec(spec)
|
||||
loader.exec_module(real_distutils)
|
||||
|
||||
# Copy the relevant attributes
|
||||
try:
|
||||
__revision__ = real_distutils.__revision__
|
||||
except AttributeError:
|
||||
pass
|
||||
__version__ = real_distutils.__version__
|
||||
|
||||
from distutils import dist, sysconfig # isort:skip
|
||||
|
||||
try:
|
||||
basestring
|
||||
except NameError:
|
||||
basestring = str
|
||||
|
||||
# patch build_ext (distutils doesn't know how to get the libs directory
|
||||
# path on windows - it hardcodes the paths around the patched sys.prefix)
|
||||
|
||||
if sys.platform == "win32":
|
||||
from distutils.command.build_ext import build_ext as old_build_ext
|
||||
|
||||
class build_ext(old_build_ext):
|
||||
def finalize_options(self):
|
||||
if self.library_dirs is None:
|
||||
self.library_dirs = []
|
||||
elif isinstance(self.library_dirs, basestring):
|
||||
self.library_dirs = self.library_dirs.split(os.pathsep)
|
||||
|
||||
self.library_dirs.insert(0, os.path.join(sys.real_prefix, "Libs"))
|
||||
old_build_ext.finalize_options(self)
|
||||
|
||||
from distutils.command import build_ext as build_ext_module
|
||||
|
||||
build_ext_module.build_ext = build_ext
|
||||
|
||||
# distutils.dist patches:
|
||||
|
||||
old_find_config_files = dist.Distribution.find_config_files
|
||||
|
||||
|
||||
def find_config_files(self):
|
||||
found = old_find_config_files(self)
|
||||
if os.name == "posix":
|
||||
user_filename = ".pydistutils.cfg"
|
||||
else:
|
||||
user_filename = "pydistutils.cfg"
|
||||
user_filename = os.path.join(sys.prefix, user_filename)
|
||||
if os.path.isfile(user_filename):
|
||||
for item in list(found):
|
||||
if item.endswith("pydistutils.cfg"):
|
||||
found.remove(item)
|
||||
found.append(user_filename)
|
||||
return found
|
||||
|
||||
|
||||
dist.Distribution.find_config_files = find_config_files
|
||||
|
||||
# distutils.sysconfig patches:
|
||||
|
||||
old_get_python_inc = sysconfig.get_python_inc
|
||||
|
||||
|
||||
def sysconfig_get_python_inc(plat_specific=0, prefix=None):
|
||||
if prefix is None:
|
||||
prefix = sys.real_prefix
|
||||
return old_get_python_inc(plat_specific, prefix)
|
||||
|
||||
|
||||
sysconfig_get_python_inc.__doc__ = old_get_python_inc.__doc__
|
||||
sysconfig.get_python_inc = sysconfig_get_python_inc
|
||||
|
||||
old_get_python_lib = sysconfig.get_python_lib
|
||||
|
||||
|
||||
def sysconfig_get_python_lib(plat_specific=0, standard_lib=0, prefix=None):
|
||||
if standard_lib and prefix is None:
|
||||
prefix = sys.real_prefix
|
||||
return old_get_python_lib(plat_specific, standard_lib, prefix)
|
||||
|
||||
|
||||
sysconfig_get_python_lib.__doc__ = old_get_python_lib.__doc__
|
||||
sysconfig.get_python_lib = sysconfig_get_python_lib
|
||||
|
||||
old_get_config_vars = sysconfig.get_config_vars
|
||||
|
||||
|
||||
def sysconfig_get_config_vars(*args):
|
||||
real_vars = old_get_config_vars(*args)
|
||||
if sys.platform == "win32":
|
||||
lib_dir = os.path.join(sys.real_prefix, "libs")
|
||||
if isinstance(real_vars, dict) and "LIBDIR" not in real_vars:
|
||||
real_vars["LIBDIR"] = lib_dir # asked for all
|
||||
elif isinstance(real_vars, list) and "LIBDIR" in args:
|
||||
real_vars = real_vars + [lib_dir] # asked for list
|
||||
return real_vars
|
||||
|
||||
|
||||
sysconfig_get_config_vars.__doc__ = old_get_config_vars.__doc__
|
||||
sysconfig.get_config_vars = sysconfig_get_config_vars
|
|
@ -1,6 +0,0 @@
|
|||
# This is a config file local to this virtualenv installation
|
||||
# You may include options that will be used by all distutils commands,
|
||||
# and by easy_install. For instance:
|
||||
#
|
||||
# [easy_install]
|
||||
# find_links = http://mylocalsite
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/encodings
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/enum.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/fnmatch.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/functools.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/genericpath.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/hashlib.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/heapq.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/hmac.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/imp.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/importlib
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/io.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/keyword.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/lib-dynload
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/linecache.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/locale.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/ntpath.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/operator.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/os.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/posixpath.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/random.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/re.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/reprlib.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/rlcompleter.py
|
|
@ -1 +0,0 @@
|
|||
/home/amnesia/.pyenv/versions/3.7.4/lib/python3.7/shutil.py
|
|
@ -1,115 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Cipher/AES.py : AES
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
"""AES symmetric cipher
|
||||
|
||||
AES `(Advanced Encryption Standard)`__ is a symmetric block cipher standardized
|
||||
by NIST_ . It has a fixed data block size of 16 bytes.
|
||||
Its keys can be 128, 192, or 256 bits long.
|
||||
|
||||
AES is very fast and secure, and it is the de facto standard for symmetric
|
||||
encryption.
|
||||
|
||||
As an example, encryption can be done as follows:
|
||||
|
||||
>>> from Crypto.Cipher import AES
|
||||
>>> from Crypto import Random
|
||||
>>>
|
||||
>>> key = b'Sixteen byte key'
|
||||
>>> iv = Random.new().read(AES.block_size)
|
||||
>>> cipher = AES.new(key, AES.MODE_CFB, iv)
|
||||
>>> msg = iv + cipher.encrypt(b'Attack at dawn')
|
||||
|
||||
.. __: http://en.wikipedia.org/wiki/Advanced_Encryption_Standard
|
||||
.. _NIST: http://csrc.nist.gov/publications/fips/fips197/fips-197.pdf
|
||||
|
||||
:undocumented: __revision__, __package__
|
||||
"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
from Crypto.Cipher import blockalgo
|
||||
from Crypto.Cipher import _AES
|
||||
|
||||
class AESCipher (blockalgo.BlockAlgo):
|
||||
"""AES cipher object"""
|
||||
|
||||
def __init__(self, key, *args, **kwargs):
|
||||
"""Initialize an AES cipher object
|
||||
|
||||
See also `new()` at the module level."""
|
||||
blockalgo.BlockAlgo.__init__(self, _AES, key, *args, **kwargs)
|
||||
|
||||
def new(key, *args, **kwargs):
|
||||
"""Create a new AES cipher
|
||||
|
||||
:Parameters:
|
||||
key : byte string
|
||||
The secret key to use in the symmetric cipher.
|
||||
It must be 16 (*AES-128*), 24 (*AES-192*), or 32 (*AES-256*) bytes long.
|
||||
:Keywords:
|
||||
mode : a *MODE_** constant
|
||||
The chaining mode to use for encryption or decryption.
|
||||
Default is `MODE_ECB`.
|
||||
IV : byte string
|
||||
The initialization vector to use for encryption or decryption.
|
||||
|
||||
It is ignored for `MODE_ECB` and `MODE_CTR`.
|
||||
|
||||
For `MODE_OPENPGP`, IV must be `block_size` bytes long for encryption
|
||||
and `block_size` +2 bytes for decryption (in the latter case, it is
|
||||
actually the *encrypted* IV which was prefixed to the ciphertext).
|
||||
It is mandatory.
|
||||
|
||||
For all other modes, it must be `block_size` bytes longs. It is optional and
|
||||
when not present it will be given a default value of all zeroes.
|
||||
counter : callable
|
||||
(*Only* `MODE_CTR`). A stateful function that returns the next
|
||||
*counter block*, which is a byte string of `block_size` bytes.
|
||||
For better performance, use `Crypto.Util.Counter`.
|
||||
segment_size : integer
|
||||
(*Only* `MODE_CFB`).The number of bits the plaintext and ciphertext
|
||||
are segmented in.
|
||||
It must be a multiple of 8. If 0 or not specified, it will be assumed to be 8.
|
||||
|
||||
:Return: an `AESCipher` object
|
||||
"""
|
||||
return AESCipher(key, *args, **kwargs)
|
||||
|
||||
#: Electronic Code Book (ECB). See `blockalgo.MODE_ECB`.
|
||||
MODE_ECB = 1
|
||||
#: Cipher-Block Chaining (CBC). See `blockalgo.MODE_CBC`.
|
||||
MODE_CBC = 2
|
||||
#: Cipher FeedBack (CFB). See `blockalgo.MODE_CFB`.
|
||||
MODE_CFB = 3
|
||||
#: This mode should not be used.
|
||||
MODE_PGP = 4
|
||||
#: Output FeedBack (OFB). See `blockalgo.MODE_OFB`.
|
||||
MODE_OFB = 5
|
||||
#: CounTer Mode (CTR). See `blockalgo.MODE_CTR`.
|
||||
MODE_CTR = 6
|
||||
#: OpenPGP Mode. See `blockalgo.MODE_OPENPGP`.
|
||||
MODE_OPENPGP = 7
|
||||
#: Size of a data block (in bytes)
|
||||
block_size = 16
|
||||
#: Size of a key (in bytes)
|
||||
key_size = ( 16, 24, 32 )
|
||||
|
|
@ -1,130 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Cipher/ARC2.py : ARC2.py
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
"""RC2 symmetric cipher
|
||||
|
||||
RC2_ (Rivest's Cipher version 2) is a symmetric block cipher designed
|
||||
by Ron Rivest in 1987. The cipher started as a proprietary design,
|
||||
that was reverse engineered and anonymously posted on Usenet in 1996.
|
||||
For this reason, the algorithm was first called *Alleged* RC2 (ARC2),
|
||||
since the company that owned RC2 (RSA Data Inc.) did not confirm whether
|
||||
the details leaked into public domain were really correct.
|
||||
|
||||
The company eventually published its full specification in RFC2268_.
|
||||
|
||||
RC2 has a fixed data block size of 8 bytes. Length of its keys can vary from
|
||||
8 to 128 bits. One particular property of RC2 is that the actual
|
||||
cryptographic strength of the key (*effective key length*) can be reduced
|
||||
via a parameter.
|
||||
|
||||
Even though RC2 is not cryptographically broken, it has not been analyzed as
|
||||
thoroughly as AES, which is also faster than RC2.
|
||||
|
||||
New designs should not use RC2.
|
||||
|
||||
As an example, encryption can be done as follows:
|
||||
|
||||
>>> from Crypto.Cipher import ARC2
|
||||
>>> from Crypto import Random
|
||||
>>>
|
||||
>>> key = b'Sixteen byte key'
|
||||
>>> iv = Random.new().read(ARC2.block_size)
|
||||
>>> cipher = ARC2.new(key, ARC2.MODE_CFB, iv)
|
||||
>>> msg = iv + cipher.encrypt(b'Attack at dawn')
|
||||
|
||||
.. _RC2: http://en.wikipedia.org/wiki/RC2
|
||||
.. _RFC2268: http://tools.ietf.org/html/rfc2268
|
||||
|
||||
:undocumented: __revision__, __package__
|
||||
"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
from Crypto.Cipher import blockalgo
|
||||
from Crypto.Cipher import _ARC2
|
||||
|
||||
class RC2Cipher (blockalgo.BlockAlgo):
|
||||
"""RC2 cipher object"""
|
||||
|
||||
def __init__(self, key, *args, **kwargs):
|
||||
"""Initialize an ARC2 cipher object
|
||||
|
||||
See also `new()` at the module level."""
|
||||
blockalgo.BlockAlgo.__init__(self, _ARC2, key, *args, **kwargs)
|
||||
|
||||
def new(key, *args, **kwargs):
|
||||
"""Create a new RC2 cipher
|
||||
|
||||
:Parameters:
|
||||
key : byte string
|
||||
The secret key to use in the symmetric cipher.
|
||||
Its length can vary from 1 to 128 bytes.
|
||||
:Keywords:
|
||||
mode : a *MODE_** constant
|
||||
The chaining mode to use for encryption or decryption.
|
||||
Default is `MODE_ECB`.
|
||||
IV : byte string
|
||||
The initialization vector to use for encryption or decryption.
|
||||
|
||||
It is ignored for `MODE_ECB` and `MODE_CTR`.
|
||||
|
||||
For `MODE_OPENPGP`, IV must be `block_size` bytes long for encryption
|
||||
and `block_size` +2 bytes for decryption (in the latter case, it is
|
||||
actually the *encrypted* IV which was prefixed to the ciphertext).
|
||||
It is mandatory.
|
||||
|
||||
For all other modes, it must be `block_size` bytes longs. It is optional and
|
||||
when not present it will be given a default value of all zeroes.
|
||||
counter : callable
|
||||
(*Only* `MODE_CTR`). A stateful function that returns the next
|
||||
*counter block*, which is a byte string of `block_size` bytes.
|
||||
For better performance, use `Crypto.Util.Counter`.
|
||||
segment_size : integer
|
||||
(*Only* `MODE_CFB`).The number of bits the plaintext and ciphertext
|
||||
are segmented in.
|
||||
It must be a multiple of 8. If 0 or not specified, it will be assumed to be 8.
|
||||
effective_keylen : integer
|
||||
Maximum cryptographic strength of the key, in bits.
|
||||
It can vary from 0 to 1024. The default value is 1024.
|
||||
|
||||
:Return: an `RC2Cipher` object
|
||||
"""
|
||||
return RC2Cipher(key, *args, **kwargs)
|
||||
|
||||
#: Electronic Code Book (ECB). See `blockalgo.MODE_ECB`.
|
||||
MODE_ECB = 1
|
||||
#: Cipher-Block Chaining (CBC). See `blockalgo.MODE_CBC`.
|
||||
MODE_CBC = 2
|
||||
#: Cipher FeedBack (CFB). See `blockalgo.MODE_CFB`.
|
||||
MODE_CFB = 3
|
||||
#: This mode should not be used.
|
||||
MODE_PGP = 4
|
||||
#: Output FeedBack (OFB). See `blockalgo.MODE_OFB`.
|
||||
MODE_OFB = 5
|
||||
#: CounTer Mode (CTR). See `blockalgo.MODE_CTR`.
|
||||
MODE_CTR = 6
|
||||
#: OpenPGP Mode. See `blockalgo.MODE_OPENPGP`.
|
||||
MODE_OPENPGP = 7
|
||||
#: Size of a data block (in bytes)
|
||||
block_size = 8
|
||||
#: Size of a key (in bytes)
|
||||
key_size = range(1,16+1)
|
||||
|
|
@ -1,120 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Cipher/ARC4.py : ARC4
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
"""ARC4 symmetric cipher
|
||||
|
||||
ARC4_ (Alleged RC4) is an implementation of RC4 (Rivest's Cipher version 4),
|
||||
a symmetric stream cipher designed by Ron Rivest in 1987.
|
||||
|
||||
The cipher started as a proprietary design, that was reverse engineered and
|
||||
anonymously posted on Usenet in 1994. The company that owns RC4 (RSA Data
|
||||
Inc.) never confirmed the correctness of the leaked algorithm.
|
||||
|
||||
Unlike RC2, the company has never published the full specification of RC4,
|
||||
of whom it still holds the trademark.
|
||||
|
||||
ARC4 keys can vary in length from 40 to 2048 bits.
|
||||
|
||||
One problem of ARC4 is that it does not take a nonce or an IV. If it is required
|
||||
to encrypt multiple messages with the same long-term key, a distinct
|
||||
independent nonce must be created for each message, and a short-term key must
|
||||
be derived from the combination of the long-term key and the nonce.
|
||||
Due to the weak key scheduling algorithm of RC2, the combination must be carried
|
||||
out with a complex function (e.g. a cryptographic hash) and not by simply
|
||||
concatenating key and nonce.
|
||||
|
||||
New designs should not use ARC4. A good alternative is AES
|
||||
(`Crypto.Cipher.AES`) in any of the modes that turn it into a stream cipher (OFB, CFB, or CTR).
|
||||
|
||||
As an example, encryption can be done as follows:
|
||||
|
||||
>>> from Crypto.Cipher import ARC4
|
||||
>>> from Crypto.Hash import SHA
|
||||
>>> from Crypto import Random
|
||||
>>>
|
||||
>>> key = b'Very long and confidential key'
|
||||
>>> nonce = Random.new().read(16)
|
||||
>>> tempkey = SHA.new(key+nonce).digest()
|
||||
>>> cipher = ARC4.new(tempkey)
|
||||
>>> msg = nonce + cipher.encrypt(b'Open the pod bay doors, HAL')
|
||||
|
||||
.. _ARC4: http://en.wikipedia.org/wiki/RC4
|
||||
|
||||
:undocumented: __revision__, __package__
|
||||
"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
from Crypto.Cipher import _ARC4
|
||||
|
||||
class ARC4Cipher:
|
||||
"""ARC4 cipher object"""
|
||||
|
||||
|
||||
def __init__(self, key, *args, **kwargs):
|
||||
"""Initialize an ARC4 cipher object
|
||||
|
||||
See also `new()` at the module level."""
|
||||
|
||||
self._cipher = _ARC4.new(key, *args, **kwargs)
|
||||
self.block_size = self._cipher.block_size
|
||||
self.key_size = self._cipher.key_size
|
||||
|
||||
def encrypt(self, plaintext):
|
||||
"""Encrypt a piece of data.
|
||||
|
||||
:Parameters:
|
||||
plaintext : byte string
|
||||
The piece of data to encrypt. It can be of any size.
|
||||
:Return: the encrypted data (byte string, as long as the
|
||||
plaintext).
|
||||
"""
|
||||
return self._cipher.encrypt(plaintext)
|
||||
|
||||
def decrypt(self, ciphertext):
|
||||
"""Decrypt a piece of data.
|
||||
|
||||
:Parameters:
|
||||
ciphertext : byte string
|
||||
The piece of data to decrypt. It can be of any size.
|
||||
:Return: the decrypted data (byte string, as long as the
|
||||
ciphertext).
|
||||
"""
|
||||
return self._cipher.decrypt(ciphertext)
|
||||
|
||||
def new(key, *args, **kwargs):
|
||||
"""Create a new ARC4 cipher
|
||||
|
||||
:Parameters:
|
||||
key : byte string
|
||||
The secret key to use in the symmetric cipher.
|
||||
It can have any length, with a minimum of 40 bytes.
|
||||
Its cryptograpic strength is always capped to 2048 bits (256 bytes).
|
||||
|
||||
:Return: an `ARC4Cipher` object
|
||||
"""
|
||||
return ARC4Cipher(key, *args, **kwargs)
|
||||
|
||||
#: Size of a data block (in bytes)
|
||||
block_size = 1
|
||||
#: Size of a key (in bytes)
|
||||
key_size = range(1,256+1)
|
||||
|
|
@ -1,121 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Cipher/Blowfish.py : Blowfish
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
"""Blowfish symmetric cipher
|
||||
|
||||
Blowfish_ is a symmetric block cipher designed by Bruce Schneier.
|
||||
|
||||
It has a fixed data block size of 8 bytes and its keys can vary in length
|
||||
from 32 to 448 bits (4 to 56 bytes).
|
||||
|
||||
Blowfish is deemed secure and it is fast. However, its keys should be chosen
|
||||
to be big enough to withstand a brute force attack (e.g. at least 16 bytes).
|
||||
|
||||
As an example, encryption can be done as follows:
|
||||
|
||||
>>> from Crypto.Cipher import Blowfish
|
||||
>>> from Crypto import Random
|
||||
>>> from struct import pack
|
||||
>>>
|
||||
>>> bs = Blowfish.block_size
|
||||
>>> key = b'An arbitrarily long key'
|
||||
>>> iv = Random.new().read(bs)
|
||||
>>> cipher = Blowfish.new(key, Blowfish.MODE_CBC, iv)
|
||||
>>> plaintext = b'docendo discimus '
|
||||
>>> plen = bs - divmod(len(plaintext),bs)[1]
|
||||
>>> padding = [plen]*plen
|
||||
>>> padding = pack('b'*plen, *padding)
|
||||
>>> msg = iv + cipher.encrypt(plaintext + padding)
|
||||
|
||||
.. _Blowfish: http://www.schneier.com/blowfish.html
|
||||
|
||||
:undocumented: __revision__, __package__
|
||||
"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
from Crypto.Cipher import blockalgo
|
||||
from Crypto.Cipher import _Blowfish
|
||||
|
||||
class BlowfishCipher (blockalgo.BlockAlgo):
|
||||
"""Blowfish cipher object"""
|
||||
|
||||
def __init__(self, key, *args, **kwargs):
|
||||
"""Initialize a Blowfish cipher object
|
||||
|
||||
See also `new()` at the module level."""
|
||||
blockalgo.BlockAlgo.__init__(self, _Blowfish, key, *args, **kwargs)
|
||||
|
||||
def new(key, *args, **kwargs):
|
||||
"""Create a new Blowfish cipher
|
||||
|
||||
:Parameters:
|
||||
key : byte string
|
||||
The secret key to use in the symmetric cipher.
|
||||
Its length can vary from 4 to 56 bytes.
|
||||
:Keywords:
|
||||
mode : a *MODE_** constant
|
||||
The chaining mode to use for encryption or decryption.
|
||||
Default is `MODE_ECB`.
|
||||
IV : byte string
|
||||
The initialization vector to use for encryption or decryption.
|
||||
|
||||
It is ignored for `MODE_ECB` and `MODE_CTR`.
|
||||
|
||||
For `MODE_OPENPGP`, IV must be `block_size` bytes long for encryption
|
||||
and `block_size` +2 bytes for decryption (in the latter case, it is
|
||||
actually the *encrypted* IV which was prefixed to the ciphertext).
|
||||
It is mandatory.
|
||||
|
||||
For all other modes, it must be `block_size` bytes longs. It is optional and
|
||||
when not present it will be given a default value of all zeroes.
|
||||
counter : callable
|
||||
(*Only* `MODE_CTR`). A stateful function that returns the next
|
||||
*counter block*, which is a byte string of `block_size` bytes.
|
||||
For better performance, use `Crypto.Util.Counter`.
|
||||
segment_size : integer
|
||||
(*Only* `MODE_CFB`).The number of bits the plaintext and ciphertext
|
||||
are segmented in.
|
||||
It must be a multiple of 8. If 0 or not specified, it will be assumed to be 8.
|
||||
|
||||
:Return: a `BlowfishCipher` object
|
||||
"""
|
||||
return BlowfishCipher(key, *args, **kwargs)
|
||||
|
||||
#: Electronic Code Book (ECB). See `blockalgo.MODE_ECB`.
|
||||
MODE_ECB = 1
|
||||
#: Cipher-Block Chaining (CBC). See `blockalgo.MODE_CBC`.
|
||||
MODE_CBC = 2
|
||||
#: Cipher FeedBack (CFB). See `blockalgo.MODE_CFB`.
|
||||
MODE_CFB = 3
|
||||
#: This mode should not be used.
|
||||
MODE_PGP = 4
|
||||
#: Output FeedBack (OFB). See `blockalgo.MODE_OFB`.
|
||||
MODE_OFB = 5
|
||||
#: CounTer Mode (CTR). See `blockalgo.MODE_CTR`.
|
||||
MODE_CTR = 6
|
||||
#: OpenPGP Mode. See `blockalgo.MODE_OPENPGP`.
|
||||
MODE_OPENPGP = 7
|
||||
#: Size of a data block (in bytes)
|
||||
block_size = 8
|
||||
#: Size of a key (in bytes)
|
||||
key_size = range(4,56+1)
|
||||
|
|
@ -1,123 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Cipher/CAST.py : CAST
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
"""CAST-128 symmetric cipher
|
||||
|
||||
CAST-128_ (or CAST5) is a symmetric block cipher specified in RFC2144_.
|
||||
|
||||
It has a fixed data block size of 8 bytes. Its key can vary in length
|
||||
from 40 to 128 bits.
|
||||
|
||||
CAST is deemed to be cryptographically secure, but its usage is not widespread.
|
||||
Keys of sufficient length should be used to prevent brute force attacks
|
||||
(128 bits are recommended).
|
||||
|
||||
As an example, encryption can be done as follows:
|
||||
|
||||
>>> from Crypto.Cipher import CAST
|
||||
>>> from Crypto import Random
|
||||
>>>
|
||||
>>> key = b'Sixteen byte key'
|
||||
>>> iv = Random.new().read(CAST.block_size)
|
||||
>>> cipher = CAST.new(key, CAST.MODE_OPENPGP, iv)
|
||||
>>> plaintext = b'sona si latine loqueris '
|
||||
>>> msg = cipher.encrypt(plaintext)
|
||||
>>>
|
||||
...
|
||||
>>> eiv = msg[:CAST.block_size+2]
|
||||
>>> ciphertext = msg[CAST.block_size+2:]
|
||||
>>> cipher = CAST.new(key, CAST.MODE_OPENPGP, eiv)
|
||||
>>> print cipher.decrypt(ciphertext)
|
||||
|
||||
.. _CAST-128: http://en.wikipedia.org/wiki/CAST-128
|
||||
.. _RFC2144: http://tools.ietf.org/html/rfc2144
|
||||
|
||||
:undocumented: __revision__, __package__
|
||||
"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
from Crypto.Cipher import blockalgo
|
||||
from Crypto.Cipher import _CAST
|
||||
|
||||
class CAST128Cipher(blockalgo.BlockAlgo):
|
||||
"""CAST-128 cipher object"""
|
||||
|
||||
def __init__(self, key, *args, **kwargs):
|
||||
"""Initialize a CAST-128 cipher object
|
||||
|
||||
See also `new()` at the module level."""
|
||||
blockalgo.BlockAlgo.__init__(self, _CAST, key, *args, **kwargs)
|
||||
|
||||
def new(key, *args, **kwargs):
|
||||
"""Create a new CAST-128 cipher
|
||||
|
||||
:Parameters:
|
||||
key : byte string
|
||||
The secret key to use in the symmetric cipher.
|
||||
Its length may vary from 5 to 16 bytes.
|
||||
:Keywords:
|
||||
mode : a *MODE_** constant
|
||||
The chaining mode to use for encryption or decryption.
|
||||
Default is `MODE_ECB`.
|
||||
IV : byte string
|
||||
The initialization vector to use for encryption or decryption.
|
||||
|
||||
It is ignored for `MODE_ECB` and `MODE_CTR`.
|
||||
|
||||
For `MODE_OPENPGP`, IV must be `block_size` bytes long for encryption
|
||||
and `block_size` +2 bytes for decryption (in the latter case, it is
|
||||
actually the *encrypted* IV which was prefixed to the ciphertext).
|
||||
It is mandatory.
|
||||
|
||||
For all other modes, it must be `block_size` bytes longs. It is optional and
|
||||
when not present it will be given a default value of all zeroes.
|
||||
counter : callable
|
||||
(*Only* `MODE_CTR`). A stateful function that returns the next
|
||||
*counter block*, which is a byte string of `block_size` bytes.
|
||||
For better performance, use `Crypto.Util.Counter`.
|
||||
segment_size : integer
|
||||
(*Only* `MODE_CFB`).The number of bits the plaintext and ciphertext
|
||||
are segmented in.
|
||||
It must be a multiple of 8. If 0 or not specified, it will be assumed to be 8.
|
||||
|
||||
:Return: an `CAST128Cipher` object
|
||||
"""
|
||||
return CAST128Cipher(key, *args, **kwargs)
|
||||
|
||||
#: Electronic Code Book (ECB). See `blockalgo.MODE_ECB`.
|
||||
MODE_ECB = 1
|
||||
#: Cipher-Block Chaining (CBC). See `blockalgo.MODE_CBC`.
|
||||
MODE_CBC = 2
|
||||
#: Cipher FeedBack (CFB). See `blockalgo.MODE_CFB`.
|
||||
MODE_CFB = 3
|
||||
#: This mode should not be used.
|
||||
MODE_PGP = 4
|
||||
#: Output FeedBack (OFB). See `blockalgo.MODE_OFB`.
|
||||
MODE_OFB = 5
|
||||
#: CounTer Mode (CTR). See `blockalgo.MODE_CTR`.
|
||||
MODE_CTR = 6
|
||||
#: OpenPGP Mode. See `blockalgo.MODE_OPENPGP`.
|
||||
MODE_OPENPGP = 7
|
||||
#: Size of a data block (in bytes)
|
||||
block_size = 8
|
||||
#: Size of a key (in bytes)
|
||||
key_size = range(5,16+1)
|
|
@ -1,118 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Cipher/DES.py : DES
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
"""DES symmetric cipher
|
||||
|
||||
DES `(Data Encryption Standard)`__ is a symmetric block cipher standardized
|
||||
by NIST_ . It has a fixed data block size of 8 bytes.
|
||||
Its keys are 64 bits long, even though 8 bits were used for integrity (now they
|
||||
are ignored) and do not contribute to securty.
|
||||
|
||||
DES is cryptographically secure, but its key length is too short by nowadays
|
||||
standards and it could be brute forced with some effort.
|
||||
|
||||
DES should not be used for new designs. Use `AES`.
|
||||
|
||||
As an example, encryption can be done as follows:
|
||||
|
||||
>>> from Crypto.Cipher import DES3
|
||||
>>> from Crypto import Random
|
||||
>>>
|
||||
>>> key = b'Sixteen byte key'
|
||||
>>> iv = Random.new().read(DES3.block_size)
|
||||
>>> cipher = DES3.new(key, DES3.MODE_OFB, iv)
|
||||
>>> plaintext = b'sona si latine loqueris '
|
||||
>>> msg = iv + cipher.encrypt(plaintext)
|
||||
|
||||
.. __: http://en.wikipedia.org/wiki/Data_Encryption_Standard
|
||||
.. _NIST: http://csrc.nist.gov/publications/fips/fips46-3/fips46-3.pdf
|
||||
|
||||
:undocumented: __revision__, __package__
|
||||
"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
from Crypto.Cipher import blockalgo
|
||||
from Crypto.Cipher import _DES
|
||||
|
||||
class DESCipher(blockalgo.BlockAlgo):
|
||||
"""DES cipher object"""
|
||||
|
||||
def __init__(self, key, *args, **kwargs):
|
||||
"""Initialize a DES cipher object
|
||||
|
||||
See also `new()` at the module level."""
|
||||
blockalgo.BlockAlgo.__init__(self, _DES, key, *args, **kwargs)
|
||||
|
||||
def new(key, *args, **kwargs):
|
||||
"""Create a new DES cipher
|
||||
|
||||
:Parameters:
|
||||
key : byte string
|
||||
The secret key to use in the symmetric cipher.
|
||||
It must be 8 byte long. The parity bits will be ignored.
|
||||
:Keywords:
|
||||
mode : a *MODE_** constant
|
||||
The chaining mode to use for encryption or decryption.
|
||||
Default is `MODE_ECB`.
|
||||
IV : byte string
|
||||
The initialization vector to use for encryption or decryption.
|
||||
|
||||
It is ignored for `MODE_ECB` and `MODE_CTR`.
|
||||
|
||||
For `MODE_OPENPGP`, IV must be `block_size` bytes long for encryption
|
||||
and `block_size` +2 bytes for decryption (in the latter case, it is
|
||||
actually the *encrypted* IV which was prefixed to the ciphertext).
|
||||
It is mandatory.
|
||||
|
||||
For all other modes, it must be `block_size` bytes longs. It is optional and
|
||||
when not present it will be given a default value of all zeroes.
|
||||
counter : callable
|
||||
(*Only* `MODE_CTR`). A stateful function that returns the next
|
||||
*counter block*, which is a byte string of `block_size` bytes.
|
||||
For better performance, use `Crypto.Util.Counter`.
|
||||
segment_size : integer
|
||||
(*Only* `MODE_CFB`).The number of bits the plaintext and ciphertext
|
||||
are segmented in.
|
||||
It must be a multiple of 8. If 0 or not specified, it will be assumed to be 8.
|
||||
|
||||
:Return: an `DESCipher` object
|
||||
"""
|
||||
return DESCipher(key, *args, **kwargs)
|
||||
|
||||
#: Electronic Code Book (ECB). See `blockalgo.MODE_ECB`.
|
||||
MODE_ECB = 1
|
||||
#: Cipher-Block Chaining (CBC). See `blockalgo.MODE_CBC`.
|
||||
MODE_CBC = 2
|
||||
#: Cipher FeedBack (CFB). See `blockalgo.MODE_CFB`.
|
||||
MODE_CFB = 3
|
||||
#: This mode should not be used.
|
||||
MODE_PGP = 4
|
||||
#: Output FeedBack (OFB). See `blockalgo.MODE_OFB`.
|
||||
MODE_OFB = 5
|
||||
#: CounTer Mode (CTR). See `blockalgo.MODE_CTR`.
|
||||
MODE_CTR = 6
|
||||
#: OpenPGP Mode. See `blockalgo.MODE_OPENPGP`.
|
||||
MODE_OPENPGP = 7
|
||||
#: Size of a data block (in bytes)
|
||||
block_size = 8
|
||||
#: Size of a key (in bytes)
|
||||
key_size = 8
|
|
@ -1,133 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Cipher/DES3.py : DES3
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
"""Triple DES symmetric cipher
|
||||
|
||||
`Triple DES`__ (or TDES or TDEA or 3DES) is a symmetric block cipher standardized by NIST_.
|
||||
It has a fixed data block size of 8 bytes. Its keys are 128 (*Option 1*) or 192
|
||||
bits (*Option 2*) long.
|
||||
However, 1 out of 8 bits is used for redundancy and do not contribute to
|
||||
security. The effective key length is respectively 112 or 168 bits.
|
||||
|
||||
TDES consists of the concatenation of 3 simple `DES` ciphers.
|
||||
|
||||
The plaintext is first DES encrypted with *K1*, then decrypted with *K2*,
|
||||
and finally encrypted again with *K3*. The ciphertext is decrypted in the reverse manner.
|
||||
|
||||
The 192 bit key is a bundle of three 64 bit independent subkeys: *K1*, *K2*, and *K3*.
|
||||
|
||||
The 128 bit key is split into *K1* and *K2*, whereas *K1=K3*.
|
||||
|
||||
It is important that all subkeys are different, otherwise TDES would degrade to
|
||||
single `DES`.
|
||||
|
||||
TDES is cryptographically secure, even though it is neither as secure nor as fast
|
||||
as `AES`.
|
||||
|
||||
As an example, encryption can be done as follows:
|
||||
|
||||
>>> from Crypto.Cipher import DES
|
||||
>>> from Crypto import Random
|
||||
>>> from Crypto.Util import Counter
|
||||
>>>
|
||||
>>> key = b'-8B key-'
|
||||
>>> nonce = Random.new().read(DES.block_size/2)
|
||||
>>> ctr = Counter.new(DES.block_size*8/2, prefix=nonce)
|
||||
>>> cipher = DES.new(key, DES.MODE_CTR, counter=ctr)
|
||||
>>> plaintext = b'We are no longer the knights who say ni!'
|
||||
>>> msg = nonce + cipher.encrypt(plaintext)
|
||||
|
||||
.. __: http://en.wikipedia.org/wiki/Triple_DES
|
||||
.. _NIST: http://csrc.nist.gov/publications/nistpubs/800-67/SP800-67.pdf
|
||||
|
||||
:undocumented: __revision__, __package__
|
||||
"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
from Crypto.Cipher import blockalgo
|
||||
from Crypto.Cipher import _DES3
|
||||
|
||||
class DES3Cipher(blockalgo.BlockAlgo):
|
||||
"""TDES cipher object"""
|
||||
|
||||
def __init__(self, key, *args, **kwargs):
|
||||
"""Initialize a TDES cipher object
|
||||
|
||||
See also `new()` at the module level."""
|
||||
blockalgo.BlockAlgo.__init__(self, _DES3, key, *args, **kwargs)
|
||||
|
||||
def new(key, *args, **kwargs):
|
||||
"""Create a new TDES cipher
|
||||
|
||||
:Parameters:
|
||||
key : byte string
|
||||
The secret key to use in the symmetric cipher.
|
||||
It must be 16 or 24 bytes long. The parity bits will be ignored.
|
||||
:Keywords:
|
||||
mode : a *MODE_** constant
|
||||
The chaining mode to use for encryption or decryption.
|
||||
Default is `MODE_ECB`.
|
||||
IV : byte string
|
||||
The initialization vector to use for encryption or decryption.
|
||||
|
||||
It is ignored for `MODE_ECB` and `MODE_CTR`.
|
||||
|
||||
For `MODE_OPENPGP`, IV must be `block_size` bytes long for encryption
|
||||
and `block_size` +2 bytes for decryption (in the latter case, it is
|
||||
actually the *encrypted* IV which was prefixed to the ciphertext).
|
||||
It is mandatory.
|
||||
|
||||
For all other modes, it must be `block_size` bytes longs. It is optional and
|
||||
when not present it will be given a default value of all zeroes.
|
||||
counter : callable
|
||||
(*Only* `MODE_CTR`). A stateful function that returns the next
|
||||
*counter block*, which is a byte string of `block_size` bytes.
|
||||
For better performance, use `Crypto.Util.Counter`.
|
||||
segment_size : integer
|
||||
(*Only* `MODE_CFB`).The number of bits the plaintext and ciphertext
|
||||
are segmented in.
|
||||
It must be a multiple of 8. If 0 or not specified, it will be assumed to be 8.
|
||||
|
||||
:Attention: it is important that all 8 byte subkeys are different,
|
||||
otherwise TDES would degrade to single `DES`.
|
||||
:Return: an `DES3Cipher` object
|
||||
"""
|
||||
return DES3Cipher(key, *args, **kwargs)
|
||||
|
||||
#: Electronic Code Book (ECB). See `blockalgo.MODE_ECB`.
|
||||
MODE_ECB = 1
|
||||
#: Cipher-Block Chaining (CBC). See `blockalgo.MODE_CBC`.
|
||||
MODE_CBC = 2
|
||||
#: Cipher FeedBack (CFB). See `blockalgo.MODE_CFB`.
|
||||
MODE_CFB = 3
|
||||
#: This mode should not be used.
|
||||
MODE_PGP = 4
|
||||
#: Output FeedBack (OFB). See `blockalgo.MODE_OFB`.
|
||||
MODE_OFB = 5
|
||||
#: CounTer Mode (CTR). See `blockalgo.MODE_CTR`.
|
||||
MODE_CTR = 6
|
||||
#: OpenPGP Mode. See `blockalgo.MODE_OPENPGP`.
|
||||
MODE_OPENPGP = 7
|
||||
#: Size of a data block (in bytes)
|
||||
block_size = 8
|
||||
#: Size of a key (in bytes)
|
||||
key_size = ( 16, 24 )
|
|
@ -1,255 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Cipher/PKCS1_OAEP.py : PKCS#1 OAEP
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""RSA encryption protocol according to PKCS#1 OAEP
|
||||
|
||||
See RFC3447__ or the `original RSA Labs specification`__ .
|
||||
|
||||
This scheme is more properly called ``RSAES-OAEP``.
|
||||
|
||||
As an example, a sender may encrypt a message in this way:
|
||||
|
||||
>>> from Crypto.Cipher import PKCS1_OAEP
|
||||
>>> from Crypto.PublicKey import RSA
|
||||
>>>
|
||||
>>> message = 'To be encrypted'
|
||||
>>> key = RSA.importKey(open('pubkey.der').read())
|
||||
>>> cipher = PKCS1_OAEP.new(key)
|
||||
>>> ciphertext = cipher.encrypt(message)
|
||||
|
||||
At the receiver side, decryption can be done using the private part of
|
||||
the RSA key:
|
||||
|
||||
>>> key = RSA.importKey(open('privkey.der').read())
|
||||
>>> cipher = PKCS1_OAP.new(key)
|
||||
>>> message = cipher.decrypt(ciphertext)
|
||||
|
||||
:undocumented: __revision__, __package__
|
||||
|
||||
.. __: http://www.ietf.org/rfc/rfc3447.txt
|
||||
.. __: http://www.rsa.com/rsalabs/node.asp?id=2125.
|
||||
"""
|
||||
|
||||
|
||||
|
||||
__revision__ = "$Id$"
|
||||
__all__ = [ 'new', 'PKCS1OAEP_Cipher' ]
|
||||
|
||||
import Crypto.Signature.PKCS1_PSS
|
||||
import Crypto.Hash.SHA
|
||||
|
||||
from Crypto.Util.py3compat import *
|
||||
import Crypto.Util.number
|
||||
from Crypto.Util.number import ceil_div
|
||||
from Crypto.Util.strxor import strxor
|
||||
|
||||
class PKCS1OAEP_Cipher:
|
||||
"""This cipher can perform PKCS#1 v1.5 OAEP encryption or decryption."""
|
||||
|
||||
def __init__(self, key, hashAlgo, mgfunc, label):
|
||||
"""Initialize this PKCS#1 OAEP cipher object.
|
||||
|
||||
:Parameters:
|
||||
key : an RSA key object
|
||||
If a private half is given, both encryption and decryption are possible.
|
||||
If a public half is given, only encryption is possible.
|
||||
hashAlgo : hash object
|
||||
The hash function to use. This can be a module under `Crypto.Hash`
|
||||
or an existing hash object created from any of such modules. If not specified,
|
||||
`Crypto.Hash.SHA` (that is, SHA-1) is used.
|
||||
mgfunc : callable
|
||||
A mask generation function that accepts two parameters: a string to
|
||||
use as seed, and the lenth of the mask to generate, in bytes.
|
||||
If not specified, the standard MGF1 is used (a safe choice).
|
||||
label : string
|
||||
A label to apply to this particular encryption. If not specified,
|
||||
an empty string is used. Specifying a label does not improve
|
||||
security.
|
||||
|
||||
:attention: Modify the mask generation function only if you know what you are doing.
|
||||
Sender and receiver must use the same one.
|
||||
"""
|
||||
self._key = key
|
||||
|
||||
if hashAlgo:
|
||||
self._hashObj = hashAlgo
|
||||
else:
|
||||
self._hashObj = Crypto.Hash.SHA
|
||||
|
||||
if mgfunc:
|
||||
self._mgf = mgfunc
|
||||
else:
|
||||
self._mgf = lambda x,y: Crypto.Signature.PKCS1_PSS.MGF1(x,y,self._hashObj)
|
||||
|
||||
self._label = label
|
||||
|
||||
def can_encrypt(self):
|
||||
"""Return True/1 if this cipher object can be used for encryption."""
|
||||
return self._key.can_encrypt()
|
||||
|
||||
def can_decrypt(self):
|
||||
"""Return True/1 if this cipher object can be used for decryption."""
|
||||
return self._key.can_decrypt()
|
||||
|
||||
def encrypt(self, message):
|
||||
"""Produce the PKCS#1 OAEP encryption of a message.
|
||||
|
||||
This function is named ``RSAES-OAEP-ENCRYPT``, and is specified in
|
||||
section 7.1.1 of RFC3447.
|
||||
|
||||
:Parameters:
|
||||
message : string
|
||||
The message to encrypt, also known as plaintext. It can be of
|
||||
variable length, but not longer than the RSA modulus (in bytes)
|
||||
minus 2, minus twice the hash output size.
|
||||
|
||||
:Return: A string, the ciphertext in which the message is encrypted.
|
||||
It is as long as the RSA modulus (in bytes).
|
||||
:Raise ValueError:
|
||||
If the RSA key length is not sufficiently long to deal with the given
|
||||
message.
|
||||
"""
|
||||
# TODO: Verify the key is RSA
|
||||
|
||||
randFunc = self._key._randfunc
|
||||
|
||||
# See 7.1.1 in RFC3447
|
||||
modBits = Crypto.Util.number.size(self._key.n)
|
||||
k = ceil_div(modBits,8) # Convert from bits to bytes
|
||||
hLen = self._hashObj.digest_size
|
||||
mLen = len(message)
|
||||
|
||||
# Step 1b
|
||||
ps_len = k-mLen-2*hLen-2
|
||||
if ps_len<0:
|
||||
raise ValueError("Plaintext is too long.")
|
||||
# Step 2a
|
||||
lHash = self._hashObj.new(self._label).digest()
|
||||
# Step 2b
|
||||
ps = bchr(0x00)*ps_len
|
||||
# Step 2c
|
||||
db = lHash + ps + bchr(0x01) + message
|
||||
# Step 2d
|
||||
ros = randFunc(hLen)
|
||||
# Step 2e
|
||||
dbMask = self._mgf(ros, k-hLen-1)
|
||||
# Step 2f
|
||||
maskedDB = strxor(db, dbMask)
|
||||
# Step 2g
|
||||
seedMask = self._mgf(maskedDB, hLen)
|
||||
# Step 2h
|
||||
maskedSeed = strxor(ros, seedMask)
|
||||
# Step 2i
|
||||
em = bchr(0x00) + maskedSeed + maskedDB
|
||||
# Step 3a (OS2IP), step 3b (RSAEP), part of step 3c (I2OSP)
|
||||
m = self._key.encrypt(em, 0)[0]
|
||||
# Complete step 3c (I2OSP)
|
||||
c = bchr(0x00)*(k-len(m)) + m
|
||||
return c
|
||||
|
||||
def decrypt(self, ct):
|
||||
"""Decrypt a PKCS#1 OAEP ciphertext.
|
||||
|
||||
This function is named ``RSAES-OAEP-DECRYPT``, and is specified in
|
||||
section 7.1.2 of RFC3447.
|
||||
|
||||
:Parameters:
|
||||
ct : string
|
||||
The ciphertext that contains the message to recover.
|
||||
|
||||
:Return: A string, the original message.
|
||||
:Raise ValueError:
|
||||
If the ciphertext length is incorrect, or if the decryption does not
|
||||
succeed.
|
||||
:Raise TypeError:
|
||||
If the RSA key has no private half.
|
||||
"""
|
||||
# TODO: Verify the key is RSA
|
||||
|
||||
# See 7.1.2 in RFC3447
|
||||
modBits = Crypto.Util.number.size(self._key.n)
|
||||
k = ceil_div(modBits,8) # Convert from bits to bytes
|
||||
hLen = self._hashObj.digest_size
|
||||
|
||||
# Step 1b and 1c
|
||||
if len(ct) != k or k<hLen+2:
|
||||
raise ValueError("Ciphertext with incorrect length.")
|
||||
# Step 2a (O2SIP), 2b (RSADP), and part of 2c (I2OSP)
|
||||
m = self._key.decrypt(ct)
|
||||
# Complete step 2c (I2OSP)
|
||||
em = bchr(0x00)*(k-len(m)) + m
|
||||
# Step 3a
|
||||
lHash = self._hashObj.new(self._label).digest()
|
||||
# Step 3b
|
||||
y = em[0]
|
||||
# y must be 0, but we MUST NOT check it here in order not to
|
||||
# allow attacks like Manger's (http://dl.acm.org/citation.cfm?id=704143)
|
||||
maskedSeed = em[1:hLen+1]
|
||||
maskedDB = em[hLen+1:]
|
||||
# Step 3c
|
||||
seedMask = self._mgf(maskedDB, hLen)
|
||||
# Step 3d
|
||||
seed = strxor(maskedSeed, seedMask)
|
||||
# Step 3e
|
||||
dbMask = self._mgf(seed, k-hLen-1)
|
||||
# Step 3f
|
||||
db = strxor(maskedDB, dbMask)
|
||||
# Step 3g
|
||||
valid = 1
|
||||
one = db[hLen:].find(bchr(0x01))
|
||||
lHash1 = db[:hLen]
|
||||
if lHash1!=lHash:
|
||||
valid = 0
|
||||
if one<0:
|
||||
valid = 0
|
||||
if bord(y)!=0:
|
||||
valid = 0
|
||||
if not valid:
|
||||
raise ValueError("Incorrect decryption.")
|
||||
# Step 4
|
||||
return db[hLen+one+1:]
|
||||
|
||||
def new(key, hashAlgo=None, mgfunc=None, label=b('')):
|
||||
"""Return a cipher object `PKCS1OAEP_Cipher` that can be used to perform PKCS#1 OAEP encryption or decryption.
|
||||
|
||||
:Parameters:
|
||||
key : RSA key object
|
||||
The key to use to encrypt or decrypt the message. This is a `Crypto.PublicKey.RSA` object.
|
||||
Decryption is only possible if *key* is a private RSA key.
|
||||
hashAlgo : hash object
|
||||
The hash function to use. This can be a module under `Crypto.Hash`
|
||||
or an existing hash object created from any of such modules. If not specified,
|
||||
`Crypto.Hash.SHA` (that is, SHA-1) is used.
|
||||
mgfunc : callable
|
||||
A mask generation function that accepts two parameters: a string to
|
||||
use as seed, and the lenth of the mask to generate, in bytes.
|
||||
If not specified, the standard MGF1 is used (a safe choice).
|
||||
label : string
|
||||
A label to apply to this particular encryption. If not specified,
|
||||
an empty string is used. Specifying a label does not improve
|
||||
security.
|
||||
|
||||
:attention: Modify the mask generation function only if you know what you are doing.
|
||||
Sender and receiver must use the same one.
|
||||
"""
|
||||
return PKCS1OAEP_Cipher(key, hashAlgo, mgfunc, label)
|
||||
|
|
@ -1,226 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Cipher/PKCS1-v1_5.py : PKCS#1 v1.5
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""RSA encryption protocol according to PKCS#1 v1.5
|
||||
|
||||
See RFC3447__ or the `original RSA Labs specification`__ .
|
||||
|
||||
This scheme is more properly called ``RSAES-PKCS1-v1_5``.
|
||||
|
||||
**If you are designing a new protocol, consider using the more robust PKCS#1 OAEP.**
|
||||
|
||||
As an example, a sender may encrypt a message in this way:
|
||||
|
||||
>>> from Crypto.Cipher import PKCS1_v1_5
|
||||
>>> from Crypto.PublicKey import RSA
|
||||
>>> from Crypto.Hash import SHA
|
||||
>>>
|
||||
>>> message = 'To be encrypted'
|
||||
>>> h = SHA.new(message)
|
||||
>>>
|
||||
>>> key = RSA.importKey(open('pubkey.der').read())
|
||||
>>> cipher = PKCS1_v1_5.new(key)
|
||||
>>> ciphertext = cipher.encrypt(message+h.digest())
|
||||
|
||||
At the receiver side, decryption can be done using the private part of
|
||||
the RSA key:
|
||||
|
||||
>>> From Crypto.Hash import SHA
|
||||
>>> from Crypto import Random
|
||||
>>>
|
||||
>>> key = RSA.importKey(open('privkey.der').read())
|
||||
>>>
|
||||
>>> dsize = SHA.digest_size
|
||||
>>> sentinel = Random.new().read(15+dsize) # Let's assume that average data length is 15
|
||||
>>>
|
||||
>>> cipher = PKCS1_v1_5.new(key)
|
||||
>>> message = cipher.decrypt(ciphertext, sentinel)
|
||||
>>>
|
||||
>>> digest = SHA.new(message[:-dsize]).digest()
|
||||
>>> if digest==message[-dsize:]: # Note how we DO NOT look for the sentinel
|
||||
>>> print "Encryption was correct."
|
||||
>>> else:
|
||||
>>> print "Encryption was not correct."
|
||||
|
||||
:undocumented: __revision__, __package__
|
||||
|
||||
.. __: http://www.ietf.org/rfc/rfc3447.txt
|
||||
.. __: http://www.rsa.com/rsalabs/node.asp?id=2125.
|
||||
"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
__all__ = [ 'new', 'PKCS115_Cipher' ]
|
||||
|
||||
from Crypto.Util.number import ceil_div
|
||||
from Crypto.Util.py3compat import *
|
||||
import Crypto.Util.number
|
||||
|
||||
class PKCS115_Cipher:
|
||||
"""This cipher can perform PKCS#1 v1.5 RSA encryption or decryption."""
|
||||
|
||||
def __init__(self, key):
|
||||
"""Initialize this PKCS#1 v1.5 cipher object.
|
||||
|
||||
:Parameters:
|
||||
key : an RSA key object
|
||||
If a private half is given, both encryption and decryption are possible.
|
||||
If a public half is given, only encryption is possible.
|
||||
"""
|
||||
self._key = key
|
||||
|
||||
def can_encrypt(self):
|
||||
"""Return True if this cipher object can be used for encryption."""
|
||||
return self._key.can_encrypt()
|
||||
|
||||
def can_decrypt(self):
|
||||
"""Return True if this cipher object can be used for decryption."""
|
||||
return self._key.can_decrypt()
|
||||
|
||||
def encrypt(self, message):
|
||||
"""Produce the PKCS#1 v1.5 encryption of a message.
|
||||
|
||||
This function is named ``RSAES-PKCS1-V1_5-ENCRYPT``, and is specified in
|
||||
section 7.2.1 of RFC3447.
|
||||
For a complete example see `Crypto.Cipher.PKCS1_v1_5`.
|
||||
|
||||
:Parameters:
|
||||
message : byte string
|
||||
The message to encrypt, also known as plaintext. It can be of
|
||||
variable length, but not longer than the RSA modulus (in bytes) minus 11.
|
||||
|
||||
:Return: A byte string, the ciphertext in which the message is encrypted.
|
||||
It is as long as the RSA modulus (in bytes).
|
||||
:Raise ValueError:
|
||||
If the RSA key length is not sufficiently long to deal with the given
|
||||
message.
|
||||
|
||||
"""
|
||||
# TODO: Verify the key is RSA
|
||||
|
||||
randFunc = self._key._randfunc
|
||||
|
||||
# See 7.2.1 in RFC3447
|
||||
modBits = Crypto.Util.number.size(self._key.n)
|
||||
k = ceil_div(modBits,8) # Convert from bits to bytes
|
||||
mLen = len(message)
|
||||
|
||||
# Step 1
|
||||
if mLen > k-11:
|
||||
raise ValueError("Plaintext is too long.")
|
||||
# Step 2a
|
||||
class nonZeroRandByte:
|
||||
def __init__(self, rf): self.rf=rf
|
||||
def __call__(self, c):
|
||||
while bord(c)==0x00: c=self.rf(1)[0]
|
||||
return c
|
||||
ps = tobytes(list(map(nonZeroRandByte(randFunc), randFunc(k-mLen-3))))
|
||||
# Step 2b
|
||||
em = b('\x00\x02') + ps + bchr(0x00) + message
|
||||
# Step 3a (OS2IP), step 3b (RSAEP), part of step 3c (I2OSP)
|
||||
m = self._key.encrypt(em, 0)[0]
|
||||
# Complete step 3c (I2OSP)
|
||||
c = bchr(0x00)*(k-len(m)) + m
|
||||
return c
|
||||
|
||||
def decrypt(self, ct, sentinel):
|
||||
"""Decrypt a PKCS#1 v1.5 ciphertext.
|
||||
|
||||
This function is named ``RSAES-PKCS1-V1_5-DECRYPT``, and is specified in
|
||||
section 7.2.2 of RFC3447.
|
||||
For a complete example see `Crypto.Cipher.PKCS1_v1_5`.
|
||||
|
||||
:Parameters:
|
||||
ct : byte string
|
||||
The ciphertext that contains the message to recover.
|
||||
sentinel : any type
|
||||
The object to return to indicate that an error was detected during decryption.
|
||||
|
||||
:Return: A byte string. It is either the original message or the ``sentinel`` (in case of an error).
|
||||
:Raise ValueError:
|
||||
If the ciphertext length is incorrect
|
||||
:Raise TypeError:
|
||||
If the RSA key has no private half.
|
||||
|
||||
:attention:
|
||||
You should **never** let the party who submitted the ciphertext know that
|
||||
this function returned the ``sentinel`` value.
|
||||
Armed with such knowledge (for a fair amount of carefully crafted but invalid ciphertexts),
|
||||
an attacker is able to recontruct the plaintext of any other encryption that were carried out
|
||||
with the same RSA public key (see `Bleichenbacher's`__ attack).
|
||||
|
||||
In general, it should not be possible for the other party to distinguish
|
||||
whether processing at the server side failed because the value returned
|
||||
was a ``sentinel`` as opposed to a random, invalid message.
|
||||
|
||||
In fact, the second option is not that unlikely: encryption done according to PKCS#1 v1.5
|
||||
embeds no good integrity check. There is roughly one chance
|
||||
in 2^16 for a random ciphertext to be returned as a valid message
|
||||
(although random looking).
|
||||
|
||||
It is therefore advisabled to:
|
||||
|
||||
1. Select as ``sentinel`` a value that resembles a plausable random, invalid message.
|
||||
2. Not report back an error as soon as you detect a ``sentinel`` value.
|
||||
Put differently, you should not explicitly check if the returned value is the ``sentinel`` or not.
|
||||
3. Cover all possible errors with a single, generic error indicator.
|
||||
4. Embed into the definition of ``message`` (at the protocol level) a digest (e.g. ``SHA-1``).
|
||||
It is recommended for it to be the rightmost part ``message``.
|
||||
5. Where possible, monitor the number of errors due to ciphertexts originating from the same party,
|
||||
and slow down the rate of the requests from such party (or even blacklist it altogether).
|
||||
|
||||
**If you are designing a new protocol, consider using the more robust PKCS#1 OAEP.**
|
||||
|
||||
.. __: http://www.bell-labs.com/user/bleichen/papers/pkcs.ps
|
||||
|
||||
"""
|
||||
|
||||
# TODO: Verify the key is RSA
|
||||
|
||||
# See 7.2.1 in RFC3447
|
||||
modBits = Crypto.Util.number.size(self._key.n)
|
||||
k = ceil_div(modBits,8) # Convert from bits to bytes
|
||||
|
||||
# Step 1
|
||||
if len(ct) != k:
|
||||
raise ValueError("Ciphertext with incorrect length.")
|
||||
# Step 2a (O2SIP), 2b (RSADP), and part of 2c (I2OSP)
|
||||
m = self._key.decrypt(ct)
|
||||
# Complete step 2c (I2OSP)
|
||||
em = bchr(0x00)*(k-len(m)) + m
|
||||
# Step 3
|
||||
sep = em.find(bchr(0x00),2)
|
||||
if not em.startswith(b('\x00\x02')) or sep<10:
|
||||
return sentinel
|
||||
# Step 4
|
||||
return em[sep+1:]
|
||||
|
||||
def new(key):
|
||||
"""Return a cipher object `PKCS115_Cipher` that can be used to perform PKCS#1 v1.5 encryption or decryption.
|
||||
|
||||
:Parameters:
|
||||
key : RSA key object
|
||||
The key to use to encrypt or decrypt the message. This is a `Crypto.PublicKey.RSA` object.
|
||||
Decryption is only possible if *key* is a private RSA key.
|
||||
|
||||
"""
|
||||
return PKCS115_Cipher(key)
|
||||
|
|
@ -1,86 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Cipher/XOR.py : XOR
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
"""XOR toy cipher
|
||||
|
||||
XOR is one the simplest stream ciphers. Encryption and decryption are
|
||||
performed by XOR-ing data with a keystream made by contatenating
|
||||
the key.
|
||||
|
||||
Do not use it for real applications!
|
||||
|
||||
:undocumented: __revision__, __package__
|
||||
"""
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
from Crypto.Cipher import _XOR
|
||||
|
||||
class XORCipher:
|
||||
"""XOR cipher object"""
|
||||
|
||||
def __init__(self, key, *args, **kwargs):
|
||||
"""Initialize a XOR cipher object
|
||||
|
||||
See also `new()` at the module level."""
|
||||
self._cipher = _XOR.new(key, *args, **kwargs)
|
||||
self.block_size = self._cipher.block_size
|
||||
self.key_size = self._cipher.key_size
|
||||
|
||||
def encrypt(self, plaintext):
|
||||
"""Encrypt a piece of data.
|
||||
|
||||
:Parameters:
|
||||
plaintext : byte string
|
||||
The piece of data to encrypt. It can be of any size.
|
||||
:Return: the encrypted data (byte string, as long as the
|
||||
plaintext).
|
||||
"""
|
||||
return self._cipher.encrypt(plaintext)
|
||||
|
||||
def decrypt(self, ciphertext):
|
||||
"""Decrypt a piece of data.
|
||||
|
||||
:Parameters:
|
||||
ciphertext : byte string
|
||||
The piece of data to decrypt. It can be of any size.
|
||||
:Return: the decrypted data (byte string, as long as the
|
||||
ciphertext).
|
||||
"""
|
||||
return self._cipher.decrypt(ciphertext)
|
||||
|
||||
def new(key, *args, **kwargs):
|
||||
"""Create a new XOR cipher
|
||||
|
||||
:Parameters:
|
||||
key : byte string
|
||||
The secret key to use in the symmetric cipher.
|
||||
Its length may vary from 1 to 32 bytes.
|
||||
|
||||
:Return: an `XORCipher` object
|
||||
"""
|
||||
return XORCipher(key, *args, **kwargs)
|
||||
|
||||
#: Size of a data block (in bytes)
|
||||
block_size = 1
|
||||
#: Size of a key (in bytes)
|
||||
key_size = range(1,32+1)
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
|
@ -1,83 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""Symmetric- and asymmetric-key encryption algorithms.
|
||||
|
||||
Encryption algorithms transform plaintext in some way that
|
||||
is dependent on a key or key pair, producing ciphertext.
|
||||
|
||||
Symmetric algorithms
|
||||
--------------------
|
||||
|
||||
Encryption can easily be reversed, if (and, hopefully, only if)
|
||||
one knows the same key.
|
||||
In other words, sender and receiver share the same key.
|
||||
|
||||
The symmetric encryption modules here all support the interface described in PEP
|
||||
272, "API for Block Encryption Algorithms".
|
||||
|
||||
If you don't know which algorithm to choose, use AES because it's
|
||||
standard and has undergone a fair bit of examination.
|
||||
|
||||
======================== ======= ========================
|
||||
Module name Type Description
|
||||
======================== ======= ========================
|
||||
`Crypto.Cipher.AES` Block Advanced Encryption Standard
|
||||
`Crypto.Cipher.ARC2` Block Alleged RC2
|
||||
`Crypto.Cipher.ARC4` Stream Alleged RC4
|
||||
`Crypto.Cipher.Blowfish` Block Blowfish
|
||||
`Crypto.Cipher.CAST` Block CAST
|
||||
`Crypto.Cipher.DES` Block The Data Encryption Standard.
|
||||
Very commonly used in the past,
|
||||
but today its 56-bit keys are too small.
|
||||
`Crypto.Cipher.DES3` Block Triple DES.
|
||||
`Crypto.Cipher.XOR` Stream The simple XOR cipher.
|
||||
======================== ======= ========================
|
||||
|
||||
|
||||
Asymmetric algorithms
|
||||
---------------------
|
||||
|
||||
For asymmetric algorithms, the key to be used for decryption is totally
|
||||
different and cannot be derived in a feasible way from the key used
|
||||
for encryption. Put differently, sender and receiver each own one half
|
||||
of a key pair. The encryption key is often called ``public`` whereas
|
||||
the decryption key is called ``private``.
|
||||
|
||||
========================== =======================
|
||||
Module name Description
|
||||
========================== =======================
|
||||
`Crypto.Cipher.PKCS1_v1_5` PKCS#1 v1.5 encryption, based on RSA key pairs
|
||||
`Crypto.Cipher.PKCS1_OAEP` PKCS#1 OAEP encryption, based on RSA key pairs
|
||||
========================== =======================
|
||||
|
||||
:undocumented: __revision__, __package__, _AES, _ARC2, _ARC4, _Blowfish
|
||||
_CAST, _DES, _DES3, _XOR
|
||||
"""
|
||||
|
||||
__all__ = ['AES', 'ARC2', 'ARC4',
|
||||
'Blowfish', 'CAST', 'DES', 'DES3',
|
||||
'XOR',
|
||||
'PKCS1_v1_5', 'PKCS1_OAEP'
|
||||
]
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
|
|
@ -1,296 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Cipher/blockalgo.py
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
"""Module with definitions common to all block ciphers."""
|
||||
|
||||
import sys
|
||||
if sys.version_info[0] == 2 and sys.version_info[1] == 1:
|
||||
from Crypto.Util.py21compat import *
|
||||
from Crypto.Util.py3compat import *
|
||||
|
||||
#: *Electronic Code Book (ECB)*.
|
||||
#: This is the simplest encryption mode. Each of the plaintext blocks
|
||||
#: is directly encrypted into a ciphertext block, independently of
|
||||
#: any other block. This mode exposes frequency of symbols
|
||||
#: in your plaintext. Other modes (e.g. *CBC*) should be used instead.
|
||||
#:
|
||||
#: See `NIST SP800-38A`_ , Section 6.1 .
|
||||
#:
|
||||
#: .. _`NIST SP800-38A` : http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
|
||||
MODE_ECB = 1
|
||||
|
||||
#: *Cipher-Block Chaining (CBC)*. Each of the ciphertext blocks depends
|
||||
#: on the current and all previous plaintext blocks. An Initialization Vector
|
||||
#: (*IV*) is required.
|
||||
#:
|
||||
#: The *IV* is a data block to be transmitted to the receiver.
|
||||
#: The *IV* can be made public, but it must be authenticated by the receiver and
|
||||
#: it should be picked randomly.
|
||||
#:
|
||||
#: See `NIST SP800-38A`_ , Section 6.2 .
|
||||
#:
|
||||
#: .. _`NIST SP800-38A` : http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
|
||||
MODE_CBC = 2
|
||||
|
||||
#: *Cipher FeedBack (CFB)*. This mode is similar to CBC, but it transforms
|
||||
#: the underlying block cipher into a stream cipher. Plaintext and ciphertext
|
||||
#: are processed in *segments* of **s** bits. The mode is therefore sometimes
|
||||
#: labelled **s**-bit CFB. An Initialization Vector (*IV*) is required.
|
||||
#:
|
||||
#: When encrypting, each ciphertext segment contributes to the encryption of
|
||||
#: the next plaintext segment.
|
||||
#:
|
||||
#: This *IV* is a data block to be transmitted to the receiver.
|
||||
#: The *IV* can be made public, but it should be picked randomly.
|
||||
#: Reusing the same *IV* for encryptions done with the same key lead to
|
||||
#: catastrophic cryptographic failures.
|
||||
#:
|
||||
#: See `NIST SP800-38A`_ , Section 6.3 .
|
||||
#:
|
||||
#: .. _`NIST SP800-38A` : http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
|
||||
MODE_CFB = 3
|
||||
|
||||
#: This mode should not be used.
|
||||
MODE_PGP = 4
|
||||
|
||||
#: *Output FeedBack (OFB)*. This mode is very similar to CBC, but it
|
||||
#: transforms the underlying block cipher into a stream cipher.
|
||||
#: The keystream is the iterated block encryption of an Initialization Vector (*IV*).
|
||||
#:
|
||||
#: The *IV* is a data block to be transmitted to the receiver.
|
||||
#: The *IV* can be made public, but it should be picked randomly.
|
||||
#:
|
||||
#: Reusing the same *IV* for encryptions done with the same key lead to
|
||||
#: catastrophic cryptograhic failures.
|
||||
#:
|
||||
#: See `NIST SP800-38A`_ , Section 6.4 .
|
||||
#:
|
||||
#: .. _`NIST SP800-38A` : http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
|
||||
MODE_OFB = 5
|
||||
|
||||
#: *CounTeR (CTR)*. This mode is very similar to ECB, in that
|
||||
#: encryption of one block is done independently of all other blocks.
|
||||
#: Unlike ECB, the block *position* contributes to the encryption and no
|
||||
#: information leaks about symbol frequency.
|
||||
#:
|
||||
#: Each message block is associated to a *counter* which must be unique
|
||||
#: across all messages that get encrypted with the same key (not just within
|
||||
#: the same message). The counter is as big as the block size.
|
||||
#:
|
||||
#: Counters can be generated in several ways. The most straightword one is
|
||||
#: to choose an *initial counter block* (which can be made public, similarly
|
||||
#: to the *IV* for the other modes) and increment its lowest **m** bits by
|
||||
#: one (modulo *2^m*) for each block. In most cases, **m** is chosen to be half
|
||||
#: the block size.
|
||||
#:
|
||||
#: Reusing the same *initial counter block* for encryptions done with the same
|
||||
#: key lead to catastrophic cryptograhic failures.
|
||||
#:
|
||||
#: See `NIST SP800-38A`_ , Section 6.5 (for the mode) and Appendix B (for how
|
||||
#: to manage the *initial counter block*).
|
||||
#:
|
||||
#: .. _`NIST SP800-38A` : http://csrc.nist.gov/publications/nistpubs/800-38a/sp800-38a.pdf
|
||||
MODE_CTR = 6
|
||||
|
||||
#: OpenPGP. This mode is a variant of CFB, and it is only used in PGP and OpenPGP_ applications.
|
||||
#: An Initialization Vector (*IV*) is required.
|
||||
#:
|
||||
#: Unlike CFB, the IV is not transmitted to the receiver. Instead, the *encrypted* IV is.
|
||||
#: The IV is a random data block. Two of its bytes are duplicated to act as a checksum
|
||||
#: for the correctness of the key. The encrypted IV is therefore 2 bytes longer than
|
||||
#: the clean IV.
|
||||
#:
|
||||
#: .. _OpenPGP: http://tools.ietf.org/html/rfc4880
|
||||
MODE_OPENPGP = 7
|
||||
|
||||
def _getParameter(name, index, args, kwargs, default=None):
|
||||
"""Find a parameter in tuple and dictionary arguments a function receives"""
|
||||
param = kwargs.get(name)
|
||||
if len(args)>index:
|
||||
if param:
|
||||
raise ValueError("Parameter '%s' is specified twice" % name)
|
||||
param = args[index]
|
||||
return param or default
|
||||
|
||||
class BlockAlgo:
|
||||
"""Class modelling an abstract block cipher."""
|
||||
|
||||
def __init__(self, factory, key, *args, **kwargs):
|
||||
self.mode = _getParameter('mode', 0, args, kwargs, default=MODE_ECB)
|
||||
self.block_size = factory.block_size
|
||||
|
||||
if self.mode != MODE_OPENPGP:
|
||||
self._cipher = factory.new(key, *args, **kwargs)
|
||||
self.IV = self._cipher.IV
|
||||
else:
|
||||
# OPENPGP mode. For details, see 13.9 in RCC4880.
|
||||
#
|
||||
# A few members are specifically created for this mode:
|
||||
# - _encrypted_iv, set in this constructor
|
||||
# - _done_first_block, set to True after the first encryption
|
||||
# - _done_last_block, set to True after a partial block is processed
|
||||
|
||||
self._done_first_block = False
|
||||
self._done_last_block = False
|
||||
self.IV = _getParameter('iv', 1, args, kwargs)
|
||||
if not self.IV:
|
||||
raise ValueError("MODE_OPENPGP requires an IV")
|
||||
|
||||
# Instantiate a temporary cipher to process the IV
|
||||
IV_cipher = factory.new(key, MODE_CFB,
|
||||
b('\x00')*self.block_size, # IV for CFB
|
||||
segment_size=self.block_size*8)
|
||||
|
||||
# The cipher will be used for...
|
||||
if len(self.IV) == self.block_size:
|
||||
# ... encryption
|
||||
self._encrypted_IV = IV_cipher.encrypt(
|
||||
self.IV + self.IV[-2:] + # Plaintext
|
||||
b('\x00')*(self.block_size-2) # Padding
|
||||
)[:self.block_size+2]
|
||||
elif len(self.IV) == self.block_size+2:
|
||||
# ... decryption
|
||||
self._encrypted_IV = self.IV
|
||||
self.IV = IV_cipher.decrypt(self.IV + # Ciphertext
|
||||
b('\x00')*(self.block_size-2) # Padding
|
||||
)[:self.block_size+2]
|
||||
if self.IV[-2:] != self.IV[-4:-2]:
|
||||
raise ValueError("Failed integrity check for OPENPGP IV")
|
||||
self.IV = self.IV[:-2]
|
||||
else:
|
||||
raise ValueError("Length of IV must be %d or %d bytes for MODE_OPENPGP"
|
||||
% (self.block_size, self.block_size+2))
|
||||
|
||||
# Instantiate the cipher for the real PGP data
|
||||
self._cipher = factory.new(key, MODE_CFB,
|
||||
self._encrypted_IV[-self.block_size:],
|
||||
segment_size=self.block_size*8)
|
||||
|
||||
def encrypt(self, plaintext):
|
||||
"""Encrypt data with the key and the parameters set at initialization.
|
||||
|
||||
The cipher object is stateful; encryption of a long block
|
||||
of data can be broken up in two or more calls to `encrypt()`.
|
||||
That is, the statement:
|
||||
|
||||
>>> c.encrypt(a) + c.encrypt(b)
|
||||
|
||||
is always equivalent to:
|
||||
|
||||
>>> c.encrypt(a+b)
|
||||
|
||||
That also means that you cannot reuse an object for encrypting
|
||||
or decrypting other data with the same key.
|
||||
|
||||
This function does not perform any padding.
|
||||
|
||||
- For `MODE_ECB`, `MODE_CBC`, and `MODE_OFB`, *plaintext* length
|
||||
(in bytes) must be a multiple of *block_size*.
|
||||
|
||||
- For `MODE_CFB`, *plaintext* length (in bytes) must be a multiple
|
||||
of *segment_size*/8.
|
||||
|
||||
- For `MODE_CTR`, *plaintext* can be of any length.
|
||||
|
||||
- For `MODE_OPENPGP`, *plaintext* must be a multiple of *block_size*,
|
||||
unless it is the last chunk of the message.
|
||||
|
||||
:Parameters:
|
||||
plaintext : byte string
|
||||
The piece of data to encrypt.
|
||||
:Return:
|
||||
the encrypted data, as a byte string. It is as long as
|
||||
*plaintext* with one exception: when encrypting the first message
|
||||
chunk with `MODE_OPENPGP`, the encypted IV is prepended to the
|
||||
returned ciphertext.
|
||||
"""
|
||||
|
||||
if self.mode == MODE_OPENPGP:
|
||||
padding_length = (self.block_size - len(plaintext) % self.block_size) % self.block_size
|
||||
if padding_length>0:
|
||||
# CFB mode requires ciphertext to have length multiple of block size,
|
||||
# but PGP mode allows the last block to be shorter
|
||||
if self._done_last_block:
|
||||
raise ValueError("Only the last chunk is allowed to have length not multiple of %d bytes",
|
||||
self.block_size)
|
||||
self._done_last_block = True
|
||||
padded = plaintext + b('\x00')*padding_length
|
||||
res = self._cipher.encrypt(padded)[:len(plaintext)]
|
||||
else:
|
||||
res = self._cipher.encrypt(plaintext)
|
||||
if not self._done_first_block:
|
||||
res = self._encrypted_IV + res
|
||||
self._done_first_block = True
|
||||
return res
|
||||
|
||||
return self._cipher.encrypt(plaintext)
|
||||
|
||||
def decrypt(self, ciphertext):
|
||||
"""Decrypt data with the key and the parameters set at initialization.
|
||||
|
||||
The cipher object is stateful; decryption of a long block
|
||||
of data can be broken up in two or more calls to `decrypt()`.
|
||||
That is, the statement:
|
||||
|
||||
>>> c.decrypt(a) + c.decrypt(b)
|
||||
|
||||
is always equivalent to:
|
||||
|
||||
>>> c.decrypt(a+b)
|
||||
|
||||
That also means that you cannot reuse an object for encrypting
|
||||
or decrypting other data with the same key.
|
||||
|
||||
This function does not perform any padding.
|
||||
|
||||
- For `MODE_ECB`, `MODE_CBC`, and `MODE_OFB`, *ciphertext* length
|
||||
(in bytes) must be a multiple of *block_size*.
|
||||
|
||||
- For `MODE_CFB`, *ciphertext* length (in bytes) must be a multiple
|
||||
of *segment_size*/8.
|
||||
|
||||
- For `MODE_CTR`, *ciphertext* can be of any length.
|
||||
|
||||
- For `MODE_OPENPGP`, *plaintext* must be a multiple of *block_size*,
|
||||
unless it is the last chunk of the message.
|
||||
|
||||
:Parameters:
|
||||
ciphertext : byte string
|
||||
The piece of data to decrypt.
|
||||
:Return: the decrypted data (byte string, as long as *ciphertext*).
|
||||
"""
|
||||
if self.mode == MODE_OPENPGP:
|
||||
padding_length = (self.block_size - len(ciphertext) % self.block_size) % self.block_size
|
||||
if padding_length>0:
|
||||
# CFB mode requires ciphertext to have length multiple of block size,
|
||||
# but PGP mode allows the last block to be shorter
|
||||
if self._done_last_block:
|
||||
raise ValueError("Only the last chunk is allowed to have length not multiple of %d bytes",
|
||||
self.block_size)
|
||||
self._done_last_block = True
|
||||
padded = ciphertext + b('\x00')*padding_length
|
||||
res = self._cipher.decrypt(padded)[:len(ciphertext)]
|
||||
else:
|
||||
res = self._cipher.decrypt(ciphertext)
|
||||
return res
|
||||
|
||||
return self._cipher.decrypt(ciphertext)
|
||||
|
|
@ -1,212 +0,0 @@
|
|||
# HMAC.py - Implements the HMAC algorithm as described by RFC 2104.
|
||||
#
|
||||
# ===================================================================
|
||||
# Portions Copyright (c) 2001, 2002, 2003 Python Software Foundation;
|
||||
# All Rights Reserved
|
||||
#
|
||||
# This file contains code from the Python 2.2 hmac.py module (the
|
||||
# "Original Code"), with modifications made after it was incorporated
|
||||
# into PyCrypto (the "Modifications").
|
||||
#
|
||||
# To the best of our knowledge, the Python Software Foundation is the
|
||||
# copyright holder of the Original Code, and has licensed it under the
|
||||
# Python 2.2 license. See the file LEGAL/copy/LICENSE.python-2.2 for
|
||||
# details.
|
||||
#
|
||||
# The Modifications to this file are dedicated to the public domain.
|
||||
# To the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever. No rights are
|
||||
# reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
|
||||
"""HMAC (Hash-based Message Authentication Code) algorithm
|
||||
|
||||
HMAC is a MAC defined in RFC2104_ and FIPS-198_ and constructed using
|
||||
a cryptograpic hash algorithm.
|
||||
It is usually named *HMAC-X*, where *X* is the hash algorithm; for
|
||||
instance *HMAC-SHA1* or *HMAC-MD5*.
|
||||
|
||||
The strength of an HMAC depends on:
|
||||
|
||||
- the strength of the hash algorithm
|
||||
- the length and entropy of the secret key
|
||||
|
||||
An example of possible usage is the following:
|
||||
|
||||
>>> from Crypto.Hash import HMAC
|
||||
>>>
|
||||
>>> secret = b'Swordfish'
|
||||
>>> h = HMAC.new(secret)
|
||||
>>> h.update(b'Hello')
|
||||
>>> print h.hexdigest()
|
||||
|
||||
.. _RFC2104: http://www.ietf.org/rfc/rfc2104.txt
|
||||
.. _FIPS-198: http://csrc.nist.gov/publications/fips/fips198/fips-198a.pdf
|
||||
"""
|
||||
|
||||
# This is just a copy of the Python 2.2 HMAC module, modified to work when
|
||||
# used on versions of Python before 2.2.
|
||||
|
||||
__revision__ = "$Id$"
|
||||
|
||||
__all__ = ['new', 'digest_size', 'HMAC' ]
|
||||
|
||||
from Crypto.Util.strxor import strxor_c
|
||||
from Crypto.Util.py3compat import *
|
||||
|
||||
#: The size of the authentication tag produced by the MAC.
|
||||
#: It matches the digest size on the underlying
|
||||
#: hashing module used.
|
||||
digest_size = None
|
||||
|
||||
class HMAC:
|
||||
"""Class that implements HMAC"""
|
||||
|
||||
#: The size of the authentication tag produced by the MAC.
|
||||
#: It matches the digest size on the underlying
|
||||
#: hashing module used.
|
||||
digest_size = None
|
||||
|
||||
def __init__(self, key, msg = None, digestmod = None):
|
||||
"""Create a new HMAC object.
|
||||
|
||||
:Parameters:
|
||||
key : byte string
|
||||
secret key for the MAC object.
|
||||
It must be long enough to match the expected security level of the
|
||||
MAC. However, there is no benefit in using keys longer than the
|
||||
`digest_size` of the underlying hash algorithm.
|
||||
msg : byte string
|
||||
The very first chunk of the message to authenticate.
|
||||
It is equivalent to an early call to `update()`. Optional.
|
||||
:Parameter digestmod:
|
||||
The hash algorithm the HMAC is based on.
|
||||
Default is `Crypto.Hash.MD5`.
|
||||
:Type digestmod:
|
||||
A hash module or object instantiated from `Crypto.Hash`
|
||||
"""
|
||||
if digestmod is None:
|
||||
from . import MD5
|
||||
digestmod = MD5
|
||||
|
||||
self.digestmod = digestmod
|
||||
self.outer = digestmod.new()
|
||||
self.inner = digestmod.new()
|
||||
try:
|
||||
self.digest_size = digestmod.digest_size
|
||||
except AttributeError:
|
||||
self.digest_size = len(self.outer.digest())
|
||||
|
||||
try:
|
||||
# The block size is 128 bytes for SHA384 and SHA512 and 64 bytes
|
||||
# for the others hash function
|
||||
blocksize = digestmod.block_size
|
||||
except AttributeError:
|
||||
blocksize = 64
|
||||
|
||||
ipad = 0x36
|
||||
opad = 0x5C
|
||||
|
||||
if len(key) > blocksize:
|
||||
key = digestmod.new(key).digest()
|
||||
|
||||
key = key + bchr(0) * (blocksize - len(key))
|
||||
self.outer.update(strxor_c(key, opad))
|
||||
self.inner.update(strxor_c(key, ipad))
|
||||
if (msg):
|
||||
self.update(msg)
|
||||
|
||||
def update(self, msg):
|
||||
"""Continue authentication of a message by consuming the next chunk of data.
|
||||
|
||||
Repeated calls are equivalent to a single call with the concatenation
|
||||
of all the arguments. In other words:
|
||||
|
||||
>>> m.update(a); m.update(b)
|
||||
|
||||
is equivalent to:
|
||||
|
||||
>>> m.update(a+b)
|
||||
|
||||
:Parameters:
|
||||
msg : byte string
|
||||
The next chunk of the message being authenticated
|
||||
"""
|
||||
|
||||
self.inner.update(msg)
|
||||
|
||||
def copy(self):
|
||||
"""Return a copy ("clone") of the MAC object.
|
||||
|
||||
The copy will have the same internal state as the original MAC
|
||||
object.
|
||||
This can be used to efficiently compute the MAC of strings that
|
||||
share a common initial substring.
|
||||
|
||||
:Returns: An `HMAC` object
|
||||
"""
|
||||
other = HMAC(b(""))
|
||||
other.digestmod = self.digestmod
|
||||
other.inner = self.inner.copy()
|
||||
other.outer = self.outer.copy()
|
||||
return other
|
||||
|
||||
def digest(self):
|
||||
"""Return the **binary** (non-printable) MAC of the message that has
|
||||
been authenticated so far.
|
||||
|
||||
This method does not change the state of the MAC object.
|
||||
You can continue updating the object after calling this function.
|
||||
|
||||
:Return: A byte string of `digest_size` bytes. It may contain non-ASCII
|
||||
characters, including null bytes.
|
||||
"""
|
||||
h = self.outer.copy()
|
||||
h.update(self.inner.digest())
|
||||
return h.digest()
|
||||
|
||||
def hexdigest(self):
|
||||
"""Return the **printable** MAC of the message that has been
|
||||
authenticated so far.
|
||||
|
||||
This method does not change the state of the MAC object.
|
||||
|
||||
:Return: A string of 2* `digest_size` bytes. It contains only
|
||||
hexadecimal ASCII digits.
|
||||
"""
|
||||
return "".join(["%02x" % bord(x)
|
||||
for x in tuple(self.digest())])
|
||||
|
||||
def new(key, msg = None, digestmod = None):
|
||||
"""Create a new HMAC object.
|
||||
|
||||
:Parameters:
|
||||
key : byte string
|
||||
key for the MAC object.
|
||||
It must be long enough to match the expected security level of the
|
||||
MAC. However, there is no benefit in using keys longer than the
|
||||
`digest_size` of the underlying hash algorithm.
|
||||
msg : byte string
|
||||
The very first chunk of the message to authenticate.
|
||||
It is equivalent to an early call to `HMAC.update()`.
|
||||
Optional.
|
||||
:Parameter digestmod:
|
||||
The hash to use to implement the HMAC. Default is `Crypto.Hash.MD5`.
|
||||
:Type digestmod:
|
||||
A hash module or instantiated object from `Crypto.Hash`
|
||||
:Returns: An `HMAC` object
|
||||
"""
|
||||
return HMAC(key, msg, digestmod)
|
||||
|
|
@ -1,91 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""MD2 cryptographic hash algorithm.
|
||||
|
||||
MD2 is specified in RFC1319_ and it produces the 128 bit digest of a message.
|
||||
|
||||
>>> from Crypto.Hash import MD2
|
||||
>>>
|
||||
>>> h = MD2.new()
|
||||
>>> h.update(b'Hello')
|
||||
>>> print h.hexdigest()
|
||||
|
||||
MD2 stand for Message Digest version 2, and it was invented by Rivest in 1989.
|
||||
|
||||
This algorithm is both slow and insecure. Do not use it for new designs.
|
||||
|
||||
.. _RFC1319: http://tools.ietf.org/html/rfc1319
|
||||
"""
|
||||
|
||||
_revision__ = "$Id$"
|
||||
|
||||
__all__ = ['new', 'digest_size', 'MD2Hash' ]
|
||||
|
||||
from Crypto.Util.py3compat import *
|
||||
from Crypto.Hash.hashalgo import HashAlgo
|
||||
|
||||
import Crypto.Hash._MD2 as _MD2
|
||||
hashFactory = _MD2
|
||||
|
||||
class MD2Hash(HashAlgo):
|
||||
"""Class that implements an MD2 hash
|
||||
|
||||
:undocumented: block_size
|
||||
"""
|
||||
|
||||
#: ASN.1 Object identifier (OID)::
|
||||
#:
|
||||
#: id-md2 OBJECT IDENTIFIER ::= {
|
||||
#: iso(1) member-body(2) us(840) rsadsi(113549)
|
||||
#: digestAlgorithm(2) 2
|
||||
#: }
|
||||
#:
|
||||
#: This value uniquely identifies the MD2 algorithm.
|
||||
oid = b('\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x02')
|
||||
|
||||
digest_size = 16
|
||||
block_size = 16
|
||||
|
||||
def __init__(self, data=None):
|
||||
HashAlgo.__init__(self, hashFactory, data)
|
||||
|
||||
def new(self, data=None):
|
||||
return MD2Hash(data)
|
||||
|
||||
def new(data=None):
|
||||
"""Return a fresh instance of the hash object.
|
||||
|
||||
:Parameters:
|
||||
data : byte string
|
||||
The very first chunk of the message to hash.
|
||||
It is equivalent to an early call to `MD2Hash.update()`.
|
||||
Optional.
|
||||
|
||||
:Return: An `MD2Hash` object
|
||||
"""
|
||||
return MD2Hash().new(data)
|
||||
|
||||
#: The size of the resulting hash in bytes.
|
||||
digest_size = MD2Hash.digest_size
|
||||
|
||||
#: The internal block size of the hash algorithm in bytes.
|
||||
block_size = MD2Hash.block_size
|
||||
|
|
@ -1,91 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""MD4 cryptographic hash algorithm.
|
||||
|
||||
MD4 is specified in RFC1320_ and produces the 128 bit digest of a message.
|
||||
|
||||
>>> from Crypto.Hash import MD4
|
||||
>>>
|
||||
>>> h = MD4.new()
|
||||
>>> h.update(b'Hello')
|
||||
>>> print h.hexdigest()
|
||||
|
||||
MD4 stand for Message Digest version 4, and it was invented by Rivest in 1990.
|
||||
|
||||
This algorithm is insecure. Do not use it for new designs.
|
||||
|
||||
.. _RFC1320: http://tools.ietf.org/html/rfc1320
|
||||
"""
|
||||
|
||||
_revision__ = "$Id$"
|
||||
|
||||
__all__ = ['new', 'digest_size', 'MD4Hash' ]
|
||||
|
||||
from Crypto.Util.py3compat import *
|
||||
from Crypto.Hash.hashalgo import HashAlgo
|
||||
|
||||
import Crypto.Hash._MD4 as _MD4
|
||||
hashFactory = _MD4
|
||||
|
||||
class MD4Hash(HashAlgo):
|
||||
"""Class that implements an MD4 hash
|
||||
|
||||
:undocumented: block_size
|
||||
"""
|
||||
|
||||
#: ASN.1 Object identifier (OID)::
|
||||
#:
|
||||
#: id-md2 OBJECT IDENTIFIER ::= {
|
||||
#: iso(1) member-body(2) us(840) rsadsi(113549)
|
||||
#: digestAlgorithm(2) 4
|
||||
#: }
|
||||
#:
|
||||
#: This value uniquely identifies the MD4 algorithm.
|
||||
oid = b('\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x04')
|
||||
|
||||
digest_size = 16
|
||||
block_size = 64
|
||||
|
||||
def __init__(self, data=None):
|
||||
HashAlgo.__init__(self, hashFactory, data)
|
||||
|
||||
def new(self, data=None):
|
||||
return MD4Hash(data)
|
||||
|
||||
def new(data=None):
|
||||
"""Return a fresh instance of the hash object.
|
||||
|
||||
:Parameters:
|
||||
data : byte string
|
||||
The very first chunk of the message to hash.
|
||||
It is equivalent to an early call to `MD4Hash.update()`.
|
||||
Optional.
|
||||
|
||||
:Return: A `MD4Hash` object
|
||||
"""
|
||||
return MD4Hash().new(data)
|
||||
|
||||
#: The size of the resulting hash in bytes.
|
||||
digest_size = MD4Hash.digest_size
|
||||
|
||||
#: The internal block size of the hash algorithm in bytes.
|
||||
block_size = MD4Hash.block_size
|
||||
|
|
@ -1,97 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""MD5 cryptographic hash algorithm.
|
||||
|
||||
MD5 is specified in RFC1321_ and produces the 128 bit digest of a message.
|
||||
|
||||
>>> from Crypto.Hash import MD5
|
||||
>>>
|
||||
>>> h = MD5.new()
|
||||
>>> h.update(b'Hello')
|
||||
>>> print h.hexdigest()
|
||||
|
||||
MD5 stand for Message Digest version 5, and it was invented by Rivest in 1991.
|
||||
|
||||
This algorithm is insecure. Do not use it for new designs.
|
||||
|
||||
.. _RFC1321: http://tools.ietf.org/html/rfc1321
|
||||
"""
|
||||
|
||||
_revision__ = "$Id$"
|
||||
|
||||
__all__ = ['new', 'digest_size', 'MD5Hash' ]
|
||||
|
||||
from Crypto.Util.py3compat import *
|
||||
from Crypto.Hash.hashalgo import HashAlgo
|
||||
|
||||
try:
|
||||
# The md5 module is deprecated in Python 2.6, so use hashlib when possible.
|
||||
import hashlib
|
||||
hashFactory = hashlib.md5
|
||||
|
||||
except ImportError:
|
||||
import md5
|
||||
hashFactory = md5
|
||||
|
||||
class MD5Hash(HashAlgo):
|
||||
"""Class that implements an MD5 hash
|
||||
|
||||
:undocumented: block_size
|
||||
"""
|
||||
|
||||
#: ASN.1 Object identifier (OID)::
|
||||
#:
|
||||
#: id-md5 OBJECT IDENTIFIER ::= {
|
||||
#: iso(1) member-body(2) us(840) rsadsi(113549)
|
||||
#: digestAlgorithm(2) 5
|
||||
#: }
|
||||
#:
|
||||
#: This value uniquely identifies the MD5 algorithm.
|
||||
oid = b('\x06\x08\x2a\x86\x48\x86\xf7\x0d\x02\x05')
|
||||
|
||||
digest_size = 16
|
||||
block_size = 64
|
||||
|
||||
def __init__(self, data=None):
|
||||
HashAlgo.__init__(self, hashFactory, data)
|
||||
|
||||
def new(self, data=None):
|
||||
return MD5Hash(data)
|
||||
|
||||
def new(data=None):
|
||||
"""Return a fresh instance of the hash object.
|
||||
|
||||
:Parameters:
|
||||
data : byte string
|
||||
The very first chunk of the message to hash.
|
||||
It is equivalent to an early call to `MD5Hash.update()`.
|
||||
Optional.
|
||||
|
||||
:Return: A `MD5Hash` object
|
||||
"""
|
||||
return MD5Hash().new(data)
|
||||
|
||||
#: The size of the resulting hash in bytes.
|
||||
digest_size = MD5Hash.digest_size
|
||||
|
||||
#: The internal block size of the hash algorithm in bytes.
|
||||
block_size = MD5Hash.block_size
|
||||
|
|
@ -1,94 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""RIPEMD-160 cryptographic hash algorithm.
|
||||
|
||||
RIPEMD-160_ produces the 160 bit digest of a message.
|
||||
|
||||
>>> from Crypto.Hash import RIPEMD
|
||||
>>>
|
||||
>>> h = RIPEMD.new()
|
||||
>>> h.update(b'Hello')
|
||||
>>> print h.hexdigest()
|
||||
|
||||
RIPEMD-160 stands for RACE Integrity Primitives Evaluation Message Digest
|
||||
with a 160 bit digest. It was invented by Dobbertin, Bosselaers, and Preneel.
|
||||
|
||||
This algorithm is considered secure, although it has not been scrutinized as
|
||||
extensively as SHA-1. Moreover, it provides an informal security level of just
|
||||
80bits.
|
||||
|
||||
.. _RIPEMD-160: http://homes.esat.kuleuven.be/~bosselae/ripemd160.html
|
||||
"""
|
||||
|
||||
_revision__ = "$Id$"
|
||||
|
||||
__all__ = ['new', 'digest_size', 'RIPEMD160Hash' ]
|
||||
|
||||
from Crypto.Util.py3compat import *
|
||||
from Crypto.Hash.hashalgo import HashAlgo
|
||||
|
||||
import Crypto.Hash._RIPEMD160 as _RIPEMD160
|
||||
hashFactory = _RIPEMD160
|
||||
|
||||
class RIPEMD160Hash(HashAlgo):
|
||||
"""Class that implements a RIPMD-160 hash
|
||||
|
||||
:undocumented: block_size
|
||||
"""
|
||||
|
||||
#: ASN.1 Object identifier (OID)::
|
||||
#:
|
||||
#: id-ripemd160 OBJECT IDENTIFIER ::= {
|
||||
#: iso(1) identified-organization(3) teletrust(36)
|
||||
#: algorithm(3) hashAlgorithm(2) ripemd160(1)
|
||||
#: }
|
||||
#:
|
||||
#: This value uniquely identifies the RIPMD-160 algorithm.
|
||||
oid = b("\x06\x05\x2b\x24\x03\x02\x01")
|
||||
|
||||
digest_size = 20
|
||||
block_size = 64
|
||||
|
||||
def __init__(self, data=None):
|
||||
HashAlgo.__init__(self, hashFactory, data)
|
||||
|
||||
def new(self, data=None):
|
||||
return RIPEMD160Hash(data)
|
||||
|
||||
def new(data=None):
|
||||
"""Return a fresh instance of the hash object.
|
||||
|
||||
:Parameters:
|
||||
data : byte string
|
||||
The very first chunk of the message to hash.
|
||||
It is equivalent to an early call to `RIPEMD160Hash.update()`.
|
||||
Optional.
|
||||
|
||||
:Return: A `RIPEMD160Hash` object
|
||||
"""
|
||||
return RIPEMD160Hash().new(data)
|
||||
|
||||
#: The size of the resulting hash in bytes.
|
||||
digest_size = RIPEMD160Hash.digest_size
|
||||
|
||||
#: The internal block size of the hash algorithm in bytes.
|
||||
block_size = RIPEMD160Hash.block_size
|
||||
|
|
@ -1,98 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""SHA-1 cryptographic hash algorithm.
|
||||
|
||||
SHA-1_ produces the 160 bit digest of a message.
|
||||
|
||||
>>> from Crypto.Hash import SHA
|
||||
>>>
|
||||
>>> h = SHA.new()
|
||||
>>> h.update(b'Hello')
|
||||
>>> print h.hexdigest()
|
||||
|
||||
*SHA* stands for Secure Hash Algorithm.
|
||||
|
||||
This algorithm is not considered secure. Do not use it for new designs.
|
||||
|
||||
.. _SHA-1: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
|
||||
"""
|
||||
|
||||
_revision__ = "$Id$"
|
||||
|
||||
__all__ = ['new', 'digest_size', 'SHA1Hash' ]
|
||||
|
||||
from Crypto.Util.py3compat import *
|
||||
from Crypto.Hash.hashalgo import HashAlgo
|
||||
|
||||
try:
|
||||
# The sha module is deprecated in Python 2.6, so use hashlib when possible.
|
||||
import hashlib
|
||||
hashFactory = hashlib.sha1
|
||||
|
||||
except ImportError:
|
||||
import sha
|
||||
hashFactory = sha
|
||||
|
||||
class SHA1Hash(HashAlgo):
|
||||
"""Class that implements a SHA-1 hash
|
||||
|
||||
:undocumented: block_size
|
||||
"""
|
||||
|
||||
#: ASN.1 Object identifier (OID)::
|
||||
#:
|
||||
#: id-sha1 OBJECT IDENTIFIER ::= {
|
||||
#: iso(1) identified-organization(3) oiw(14) secsig(3)
|
||||
#: algorithms(2) 26
|
||||
#: }
|
||||
#:
|
||||
#: This value uniquely identifies the SHA-1 algorithm.
|
||||
oid = b('\x06\x05\x2b\x0e\x03\x02\x1a')
|
||||
|
||||
digest_size = 20
|
||||
block_size = 64
|
||||
|
||||
def __init__(self, data=None):
|
||||
HashAlgo.__init__(self, hashFactory, data)
|
||||
|
||||
def new(self, data=None):
|
||||
return SHA1Hash(data)
|
||||
|
||||
def new(data=None):
|
||||
"""Return a fresh instance of the hash object.
|
||||
|
||||
:Parameters:
|
||||
data : byte string
|
||||
The very first chunk of the message to hash.
|
||||
It is equivalent to an early call to `SHA1Hash.update()`.
|
||||
Optional.
|
||||
|
||||
:Return: A `SHA1Hash` object
|
||||
"""
|
||||
return SHA1Hash().new(data)
|
||||
|
||||
#: The size of the resulting hash in bytes.
|
||||
digest_size = SHA1Hash.digest_size
|
||||
|
||||
#: The internal block size of the hash algorithm in bytes.
|
||||
block_size = SHA1Hash.block_size
|
||||
|
||||
|
|
@ -1,95 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""SHA-224 cryptographic hash algorithm.
|
||||
|
||||
SHA-224 belongs to the SHA-2_ family of cryptographic hashes.
|
||||
It produces the 224 bit digest of a message.
|
||||
|
||||
>>> from Crypto.Hash import SHA224
|
||||
>>>
|
||||
>>> h = SHA224.new()
|
||||
>>> h.update(b'Hello')
|
||||
>>> print h.hexdigest()
|
||||
|
||||
*SHA* stands for Secure Hash Algorithm.
|
||||
|
||||
.. _SHA-2: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
|
||||
"""
|
||||
|
||||
_revision__ = "$Id$"
|
||||
|
||||
__all__ = ['new', 'digest_size', 'SHA224Hash' ]
|
||||
|
||||
from Crypto.Util.py3compat import *
|
||||
from Crypto.Hash.hashalgo import HashAlgo
|
||||
|
||||
try:
|
||||
import hashlib
|
||||
hashFactory = hashlib.sha224
|
||||
|
||||
except ImportError:
|
||||
from Crypto.Hash import _SHA224
|
||||
hashFactory = _SHA224
|
||||
|
||||
class SHA224Hash(HashAlgo):
|
||||
"""Class that implements a SHA-224 hash
|
||||
|
||||
:undocumented: block_size
|
||||
"""
|
||||
|
||||
#: ASN.1 Object identifier (OID)::
|
||||
#:
|
||||
#: id-sha224 OBJECT IDENTIFIER ::= {
|
||||
#: joint-iso-itu-t(2) country(16) us(840) organization(1) gov(101) csor(3)
|
||||
#: nistalgorithm(4) hashalgs(2) 4
|
||||
#: }
|
||||
#:
|
||||
#: This value uniquely identifies the SHA-224 algorithm.
|
||||
oid = b('\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x04')
|
||||
|
||||
digest_size = 28
|
||||
block_size = 64
|
||||
|
||||
def __init__(self, data=None):
|
||||
HashAlgo.__init__(self, hashFactory, data)
|
||||
|
||||
def new(self, data=None):
|
||||
return SHA224Hash(data)
|
||||
|
||||
def new(data=None):
|
||||
"""Return a fresh instance of the hash object.
|
||||
|
||||
:Parameters:
|
||||
data : byte string
|
||||
The very first chunk of the message to hash.
|
||||
It is equivalent to an early call to `SHA224Hash.update()`.
|
||||
Optional.
|
||||
|
||||
:Return: A `SHA224Hash` object
|
||||
"""
|
||||
return SHA224Hash().new(data)
|
||||
|
||||
#: The size of the resulting hash in bytes.
|
||||
digest_size = SHA224Hash.digest_size
|
||||
|
||||
#: The internal block size of the hash algorithm in bytes.
|
||||
block_size = SHA224Hash.block_size
|
||||
|
|
@ -1,95 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""SHA-256 cryptographic hash algorithm.
|
||||
|
||||
SHA-256 belongs to the SHA-2_ family of cryptographic hashes.
|
||||
It produces the 256 bit digest of a message.
|
||||
|
||||
>>> from Crypto.Hash import SHA256
|
||||
>>>
|
||||
>>> h = SHA256.new()
|
||||
>>> h.update(b'Hello')
|
||||
>>> print h.hexdigest()
|
||||
|
||||
*SHA* stands for Secure Hash Algorithm.
|
||||
|
||||
.. _SHA-2: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
|
||||
"""
|
||||
|
||||
_revision__ = "$Id$"
|
||||
|
||||
__all__ = ['new', 'digest_size', 'SHA256Hash' ]
|
||||
|
||||
from Crypto.Util.py3compat import *
|
||||
from Crypto.Hash.hashalgo import HashAlgo
|
||||
|
||||
try:
|
||||
import hashlib
|
||||
hashFactory = hashlib.sha256
|
||||
|
||||
except ImportError:
|
||||
from Crypto.Hash import _SHA256
|
||||
hashFactory = _SHA256
|
||||
|
||||
class SHA256Hash(HashAlgo):
|
||||
"""Class that implements a SHA-256 hash
|
||||
|
||||
:undocumented: block_size
|
||||
"""
|
||||
|
||||
#: ASN.1 Object identifier (OID)::
|
||||
#:
|
||||
#: id-sha256 OBJECT IDENTIFIER ::= {
|
||||
#: joint-iso-itu-t(2) country(16) us(840) organization(1)
|
||||
#: gov(101) csor(3) nistalgorithm(4) hashalgs(2) 1
|
||||
#: }
|
||||
#:
|
||||
#: This value uniquely identifies the SHA-256 algorithm.
|
||||
oid = b('\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x01')
|
||||
|
||||
digest_size = 32
|
||||
block_size = 64
|
||||
|
||||
def __init__(self, data=None):
|
||||
HashAlgo.__init__(self, hashFactory, data)
|
||||
|
||||
def new(self, data=None):
|
||||
return SHA256Hash(data)
|
||||
|
||||
def new(data=None):
|
||||
"""Return a fresh instance of the hash object.
|
||||
|
||||
:Parameters:
|
||||
data : byte string
|
||||
The very first chunk of the message to hash.
|
||||
It is equivalent to an early call to `SHA256Hash.update()`.
|
||||
Optional.
|
||||
|
||||
:Return: A `SHA256Hash` object
|
||||
"""
|
||||
return SHA256Hash().new(data)
|
||||
|
||||
#: The size of the resulting hash in bytes.
|
||||
digest_size = SHA256Hash.digest_size
|
||||
|
||||
#: The internal block size of the hash algorithm in bytes.
|
||||
block_size = SHA256Hash.block_size
|
||||
|
|
@ -1,96 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""SHA-384 cryptographic hash algorithm.
|
||||
|
||||
SHA-384 belongs to the SHA-2_ family of cryptographic hashes.
|
||||
It produces the 384 bit digest of a message.
|
||||
|
||||
>>> from Crypto.Hash import SHA384
|
||||
>>>
|
||||
>>> h = SHA384.new()
|
||||
>>> h.update(b'Hello')
|
||||
>>> print h.hexdigest()
|
||||
|
||||
*SHA* stands for Secure Hash Algorithm.
|
||||
|
||||
.. _SHA-2: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
|
||||
"""
|
||||
|
||||
_revision__ = "$Id$"
|
||||
|
||||
__all__ = ['new', 'digest_size', 'SHA384Hash' ]
|
||||
|
||||
from Crypto.Util.py3compat import *
|
||||
from Crypto.Hash.hashalgo import HashAlgo
|
||||
|
||||
try:
|
||||
import hashlib
|
||||
hashFactory = hashlib.sha384
|
||||
|
||||
except ImportError:
|
||||
from Crypto.Hash import _SHA384
|
||||
hashFactory = _SHA384
|
||||
|
||||
class SHA384Hash(HashAlgo):
|
||||
"""Class that implements a SHA-384 hash
|
||||
|
||||
:undocumented: block_size
|
||||
"""
|
||||
|
||||
#: ASN.1 Object identifier (OID)::
|
||||
#:
|
||||
#: id-sha384 OBJECT IDENTIFIER ::= {
|
||||
#: joint-iso-itu-t(2) country(16) us(840) organization(1) gov(101) csor(3)
|
||||
#: nistalgorithm(4) hashalgs(2) 2
|
||||
#: }
|
||||
#:
|
||||
#: This value uniquely identifies the SHA-384 algorithm.
|
||||
oid = b('\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x02')
|
||||
|
||||
digest_size = 48
|
||||
block_size = 128
|
||||
|
||||
def __init__(self, data=None):
|
||||
HashAlgo.__init__(self, hashFactory, data)
|
||||
|
||||
def new(self, data=None):
|
||||
return SHA384Hash(data)
|
||||
|
||||
def new(data=None):
|
||||
"""Return a fresh instance of the hash object.
|
||||
|
||||
:Parameters:
|
||||
data : byte string
|
||||
The very first chunk of the message to hash.
|
||||
It is equivalent to an early call to `SHA384Hash.update()`.
|
||||
Optional.
|
||||
|
||||
:Return: A `SHA384Hash` object
|
||||
"""
|
||||
return SHA384Hash().new(data)
|
||||
|
||||
#: The size of the resulting hash in bytes.
|
||||
digest_size = SHA384Hash.digest_size
|
||||
|
||||
#: The internal block size of the hash algorithm in bytes.
|
||||
block_size = SHA384Hash.block_size
|
||||
|
||||
|
|
@ -1,95 +0,0 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# ===================================================================
|
||||
# The contents of this file are dedicated to the public domain. To
|
||||
# the extent that dedication to the public domain is not available,
|
||||
# everyone is granted a worldwide, perpetual, royalty-free,
|
||||
# non-exclusive license to exercise all rights associated with the
|
||||
# contents of this file for any purpose whatsoever.
|
||||
# No rights are reserved.
|
||||
#
|
||||
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||
# BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
|
||||
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
|
||||
# CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
# SOFTWARE.
|
||||
# ===================================================================
|
||||
|
||||
"""SHA-512 cryptographic hash algorithm.
|
||||
|
||||
SHA-512 belongs to the SHA-2_ family of cryptographic hashes.
|
||||
It produces the 512 bit digest of a message.
|
||||
|
||||
>>> from Crypto.Hash import SHA512
|
||||
>>>
|
||||
>>> h = SHA512.new()
|
||||
>>> h.update(b'Hello')
|
||||
>>> print h.hexdigest()
|
||||
|
||||
*SHA* stands for Secure Hash Algorithm.
|
||||
|
||||
.. _SHA-2: http://csrc.nist.gov/publications/fips/fips180-2/fips180-2.pdf
|
||||
"""
|
||||
|
||||
_revision__ = "$Id$"
|
||||
|
||||
__all__ = ['new', 'digest_size', 'SHA512Hash' ]
|
||||
|
||||
from Crypto.Util.py3compat import *
|
||||
from Crypto.Hash.hashalgo import HashAlgo
|
||||
|
||||
try:
|
||||
import hashlib
|
||||
hashFactory = hashlib.sha512
|
||||
|
||||
except ImportError:
|
||||
from Crypto.Hash import _SHA512
|
||||
hashFactory = _SHA512
|
||||
|
||||
class SHA512Hash(HashAlgo):
|
||||
"""Class that implements a SHA-512 hash
|
||||
|
||||
:undocumented: block_size
|
||||
"""
|
||||
|
||||
#: ASN.1 Object identifier (OID)::
|
||||
#:
|
||||
#: id-sha512 OBJECT IDENTIFIER ::= {
|
||||
#: joint-iso-itu-t(2)
|
||||
#: country(16) us(840) organization(1) gov(101) csor(3) nistalgorithm(4) hashalgs(2) 3
|
||||
#: }
|
||||
#:
|
||||
#: This value uniquely identifies the SHA-512 algorithm.
|
||||
oid = b('\x06\x09\x60\x86\x48\x01\x65\x03\x04\x02\x03')
|
||||
|
||||
digest_size = 64
|
||||
block_size = 128
|
||||
|
||||
def __init__(self, data=None):
|
||||
HashAlgo.__init__(self, hashFactory, data)
|
||||
|
||||
def new(self, data=None):
|
||||
return SHA512Hash(data)
|
||||
|
||||
def new(data=None):
|
||||
"""Return a fresh instance of the hash object.
|
||||
|
||||
:Parameters:
|
||||
data : byte string
|
||||
The very first chunk of the message to hash.
|
||||
It is equivalent to an early call to `SHA512Hash.update()`.
|
||||
Optional.
|
||||
|
||||
:Return: A `SHA512Hash` object
|
||||
"""
|
||||
return SHA512Hash().new(data)
|
||||
|
||||
#: The size of the resulting hash in bytes.
|
||||
digest_size = SHA512Hash.digest_size
|
||||
|
||||
#: The internal block size of the hash algorithm in bytes.
|
||||
block_size = SHA512Hash.block_size
|
||||
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show more
Loading…
Reference in a new issue