 M:: sha1-state checksum-block ( bytes state -- )
     bytes prepare-sha1-message-schedule state W<<
 
-    bytes
     state [ H>> clone ] [ W>> ] [ K>> ] tri state process-sha1-chunk ;
 
 M: sha1-state get-checksum

    [ prepare-message-schedule ]
    [ [ block-size>> ] [ H>> clone ] [ ] tri process-chunk ] bi ;

: sequence>byte-array ( seq n -- string )
    '[ _ >be ] map B{ } concat-as ;

: sha1>checksum ( sha2 -- bytes )
    H>> 4 sequence>byte-array ;

: sha-224>checksum ( sha2 -- bytes )
    H>> 7 head 4 sequence>byte-array ;

: sha-256>checksum ( sha2 -- bytes )
    H>> 4 sequence>byte-array ;

: pad-last-short-block ( state -- )
    [ bytes>> t ] [ bytes-read>> pad-last-block ] [ ] tri
    [ checksum-block ] curry each ;

PRIVATE>


    [
        A 5 bitroll-32

        B C D n sha1-f

        E

        n K nth-unsafe

        n W nth-unsafe
    ] sum-outputs 32 bits ; inline

:: process-sha1-chunk ( H W K state -- )
    80 [
        H W K inner-loop
        d H nth-unsafe e H set-nth-unsafe
