Skip to content

Educational Implementation of the SHA-256 Hash Function #12887

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

AElnamaki
Copy link

Description

Educational SHA-256 implementation in Python with type hints and doctests.

References

-https://nvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf

Checklist

  • I have read CONTRIBUTING.md.
  • This PR is all my own work.
  • All pre-commit checks pass.
  • The algorithm includes type hints.
  • The algorithm includes doctests that pass.
  • Filename follows repository naming conventions.
  • Only one algorithm file added.

@algorithms-keeper algorithms-keeper bot added require descriptive names This PR needs descriptive function and/or variable names require type hints https://docs.python.org/3/library/typing.html labels Aug 12, 2025
Copy link

@algorithms-keeper algorithms-keeper bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Click here to look at the relevant links ⬇️

🔗 Relevant Links

Repository:

Python:

Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.

algorithms-keeper commands and options

algorithms-keeper actions can be triggered by commenting on this PR:

  • @algorithms-keeper review to trigger the checks for only added pull request files
  • @algorithms-keeper review-all to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.

NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.

import sys


def ripemd160(b: bytes) -> bytes:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: b

self.buffer: list[int] = [0] * 64


def update(ctx, inp, inplen):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: update. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: ctx

Please provide type hint for the parameter: inp

Please provide type hint for the parameter: inplen

ctx.buffer[have + i] = inp[off + i]


def final(ctx):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: final. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: ctx

PADDING = [0x80] + [0] * 63


def rol(n, x):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: rol. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: n

Please provide descriptive name for the parameter: n

Please provide type hint for the parameter: x

Please provide descriptive name for the parameter: x

return ((x << n) & 0xFFFFFFFF) | (x >> (32 - n))


def f0(x, y, z):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: f0. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide type hint for the parameter: x

Please provide descriptive name for the parameter: x

Please provide type hint for the parameter: y

Please provide descriptive name for the parameter: y

Please provide type hint for the parameter: z

Please provide descriptive name for the parameter: z

return ((x & y) ^ (x & z) ^ (y & z)) & MASK32


def b2i(b: bytes) -> int:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: b

return int.from_bytes(b, byteorder="big")


def i2b(i: int) -> bytes:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: i

# =============================================================================


def is_prime(n: int) -> bool:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: n

return all(n % f != 0 for f in range(2, math.isqrt(n) + 1))


def first_n_primes(n: int):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide return type hint for the function: first_n_primes. If the function does not return a value, please provide the type hint as: def function() -> None:

Please provide descriptive name for the parameter: n

return islice(filter(is_prime, count(start=2)), n)


def frac_bin(f: float, n: int = 32) -> int:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please provide descriptive name for the parameter: f

Please provide descriptive name for the parameter: n

@algorithms-keeper algorithms-keeper bot added the awaiting reviews This PR is ready to be reviewed label Aug 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting reviews This PR is ready to be reviewed require descriptive names This PR needs descriptive function and/or variable names require type hints https://docs.python.org/3/library/typing.html
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant