Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1 add cli #2

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ name: Tests

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ requires-python = ">=3.9"
repository = "https://github.com/wfatherley/hkdfref.git"


# [project.scripts]
# hkdf = hkdfref:cli
[project.scripts]
hkdf = "hkdfref:cli"


[tool.setuptools.packages.find]
Expand Down
96 changes: 92 additions & 4 deletions src/hkdfref/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,97 @@
"""hkdfref"""
import argparse
import codecs
import hashlib

from ._hkdfref import expand, extract, hkdf, logger
from sys import stdout

from ._hkdfref import (
default_hash, default_salt, expand, extract, hkdf, logger
)

def cli():
"""command line interface"""
pass

def cli() -> None:
"""return None"""
logger.debug("cli: enter")

# configure argument parser
argument_parser = argparse.ArgumentParser(
prog="hkdf",
description="Generate secure keying material according to RFC 5869.",
epilog="Specification: https://datatracker.ietf.org/doc/html/rfc5869",
)
argument_parser.add_argument(
"ikm",
type=lambda string: string.encode("latin-1"),
action="append",
help="Input key material. Fed into HKDF as UTF-8 bytes."
)
argument_parser.add_argument(
"-s",
"--salt",
type=lambda string: string.encode("latin-1"),
default=default_salt,
help=(
"Salt for extract step. Defaults to 0x00 * 20. Fed into HKDF as"
" UTF-8 bytes."
)
)
argument_parser.add_argument(
"--hash",
help="One of the known SHAs. Used by both extract and expand"
)
argument_parser.add_argument(
"--exthash",
help="One of the known SHAs. Used only by extract."
)
argument_parser.add_argument(
"--exphash",
help="One of the known SHAs. Used only by expand."
)
argument_parser.add_argument(
"-i",
"--info",
default=b"",
type=lambda string: string.encode("latin-1"),
help="Info for expand step. Fed into HKDF as UTF-8 bytes."
)
output_length_group = argument_parser.add_mutually_exclusive_group(required=True)
output_length_group.add_argument(
"-l",
"--length",
type=int,
help="First octets to return. Return whole without."
)
output_length_group.add_argument(
"--returnall", action="store_true", help="Return entire result"
)
argument_parser.add_argument(
"--skipextract", action="store_true", help="Skip extract step"
)

# parse arguments
namespace = argument_parser.parse_args()
logger.debug("cli: args=%s", namespace)

# rebind hash functions
namespace.hash = namespace.hash or default_hash
namespace.exthash = getattr(hashlib, str(namespace.exthash), default_hash)
namespace.exphash = getattr(hashlib, str(namespace.exphash), default_hash)

# return okm
stdout.write(
codecs.encode(
hkdf(
*namespace.ikm,
salt=namespace.salt,
l=namespace.length,
info=namespace.info,
extract_hash=namespace.exthash,
expand_hash=namespace.exphash,
hash=namespace.hash,
skip_extract=namespace.skipextract,
return_all=namespace.returnall
),
"hex_codec"
).decode()
)
6 changes: 4 additions & 2 deletions src/hkdfref/_hkdfref.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from typing import Any, Callable


logger = getLogger(__name__)
logger = getLogger(__name__[1:])


default_hash = hashlib.sha512
Expand Down Expand Up @@ -39,7 +39,9 @@ def hkdf(*ikm: bytes, **kwargs: Any) -> bytes:
prk = extract(salt, *ikm, hash=extract_hash)
else:
prk = b"".join(ikm)
return expand(prk, info, l, expand_hash)
return expand(
prk, info, l, expand_hash, return_all=kwargs.get("return_all", False)
)


def expand(
Expand Down
Loading