-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for digest generation
- Loading branch information
Showing
3 changed files
with
60 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
from keri import kering | ||
from keri.core import MtrDex, coring | ||
|
||
|
||
class DigerBuilder: | ||
@staticmethod | ||
def sha256(dig): | ||
try: | ||
non_pref_dig = DigerBuilder._get_non_prefixed_digest(dig) # Temporarily remove prefix | ||
non_pref_dig = bytes.fromhex(non_pref_dig) | ||
diger = coring.Diger(raw=non_pref_dig, code=MtrDex.SHA2_256) | ||
return diger | ||
except Exception as e: | ||
raise e | ||
|
||
@staticmethod | ||
def _get_non_prefixed_digest(dig): | ||
try: | ||
prefix, digest = dig.split("_", 1) | ||
except ValueError: | ||
raise kering.ValidationError(f"Digest ({dig}) must start with prefix") | ||
except Exception: | ||
raise kering.ValidationError(f"Invalid digest {dig}") | ||
return digest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import pytest | ||
from hashlib import sha256 | ||
|
||
from keri import kering | ||
|
||
from src.verifier.core.utils import DigerBuilder | ||
|
||
|
||
def test_diger_builder(): | ||
BASE_STR = "fefUBIUhdo9032bfHf0UNONF0kubni9HnF22L0KD2".encode() | ||
dig = sha256(BASE_STR).hexdigest() | ||
dig = f"sha256_{dig}" | ||
diger = DigerBuilder.sha256(dig) | ||
assert diger.verify(BASE_STR) is True | ||
|
||
|
||
def test_diger_builder_fail(): | ||
BASE_STR = "fefUBIUhdo9032bfHf0UNONF0kubni9HnF22L0KD2".encode() | ||
WRONG_BASE_STR = "fefUBIUhdo9032bfHf0UNONF0kubni9HnF22L0KDT".encode() | ||
dig = sha256(BASE_STR).hexdigest() | ||
dig = f"sha256_{dig}" | ||
diger = DigerBuilder.sha256(dig) | ||
assert diger.verify(WRONG_BASE_STR) is False | ||
|
||
|
||
def test_diger_builder_wrong_dig(): | ||
BASE_STR = "fefUBIUhdo9032bfHf0UNONF0kubni9HnF22L0KD2".encode() | ||
dig = sha256(BASE_STR).hexdigest() | ||
# Here the dig is not prefixed | ||
with pytest.raises(kering.ValidationError) as exc_info: | ||
diger = DigerBuilder.sha256(dig) | ||
|