DEFINITION MODULE HashAlgs;

        (********************************************************)
        (*                                                      *)
        (*                    Hash algorithms                   *)
        (*                                                      *)
        (*  Programmer:         P. Moylan                       *)
        (*  Started:            14 February 2021                *)
        (*  Last edited:        4 October 2023                  *)
        (*  Status:             OK                              *)
        (*                                                      *)
        (********************************************************)


FROM SYSTEM IMPORT LOC, CARD8;
FROM BigNum IMPORT BN;
FROM VarStrings IMPORT ByteStr;


TYPE
    HashAlgorithm = (sha1, sha2_256, sha2_512);
    HashCtx;                    (* is private *)

PROCEDURE HashLength (alg: HashAlgorithm): CARDINAL;

    (* Returns the number of bytes in a hashed result.  *)
    (* In this version, can be up to 64.                *)

PROCEDURE HashInit (alg: HashAlgorithm): HashCtx;

    (* Initialization.  Creates a context for a new hash operation. *)

PROCEDURE HashUpdate (context: HashCtx;
                    VAR (*IN*) data: ARRAY OF LOC;  length: CARDINAL);

    (* Block update operation. Continues hashing,  adding more      *)
    (* data to what has already been processed for this context.    *)

PROCEDURE HashFinal (VAR (*INOUT*) context: HashCtx;
                        VAR (*OUT*) digest: ARRAY OF CARD8);

    (* Finalization. Ends a hashing operation, returning    *)
    (* the hashed result and discarding the context.        *)

END HashAlgs.

