 ! See http://factorcode.org/license.txt for BSD license.
 USING: kernel splitting grouping math sequences namespaces make
 io.binary math.bitwise checksums checksums.common
-sbufs strings combinators.smart math.ranges fry combinators ;
+sbufs strings combinators.smart math.ranges fry combinators
+accessors ;
 IN: checksums.sha2
 
 <PRIVATE
 
-SYMBOLS: vars K H process-M word-size block-size ;
+SYMBOLS: H word-size block-size ;
 
 CONSTANT: a 0
 CONSTANT: b 1

    b a pick exchange
    [ w+ a ] dip set-nth ;

: process-chunk ( M -- )
    H get clone vars set
    prepare-message-schedule block-size get [
        T1 T2 update-vars
    ] with each vars get H get [ w+ ] 2map H set ;

: seq>byte-array ( n seq -- string )
    [ swap '[ _ >be % ] each ] B{ } make ;

: preprocess-plaintext ( string big-endian? -- padded-string )
    #! pad 0x80 then 00 til 8 bytes left, then 64bit length in bits
    [ >sbuf ] dip over [
        HEX: 80 ,
        dup length HEX: 3f bitand
        calculate-pad-length 0 <string> %
        length 3 shift 8 rot [ >be ] [ >le ] if %
    ] "" make over push-all ;

: byte-array>sha2 ( byte-array -- string )
    t preprocess-plaintext
    block-size get group [ process-chunk ] each
    4 H get seq>byte-array ;

PRIVATE>



M: sha-256 checksum-bytes
    drop [
        initial-H-256 H set
        4 word-size set
        64 block-size set
        byte-array>sha2

    ] with-scope ;
