generalize sha1-interleave and move it to its own vocabulary

db4
Doug Coleman 2009-05-17 13:49:56 -05:00
parent 8b37eced05
commit ee6a8e78e7
3 changed files with 37 additions and 0 deletions

View File

@ -0,0 +1 @@
Doug Coleman

View File

@ -0,0 +1,19 @@
! Copyright (C) 2009 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: tools.test checksums.interleave checksums.sha1 ;
IN: checksums.interleave.tests
[
B{
59 155 253 205 75 163 94 115 208 42 227 92 181 19 60 232
119 65 178 131 210 48 241 230 204 216 30 156 4 215 80 84 93
206 44 1 18 128 150 153
}
] [
B{
102 83 241 12 26 250 181 76 97 200 37 117 168 74 254 48 216
170 26 58 150 150 179 24 153 146 191 225 203 127 166 167
}
sha1 interleaved-checksum
] unit-test

View File

@ -0,0 +1,17 @@
! Copyright (C) 2009 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: assocs checksums grouping kernel locals math sequences ;
IN: checksums.interleave
: seq>2seq ( seq -- seq1 seq2 )
#! { abcdefgh } -> { aceg } { bdfh }
2 group flip [ { } { } ] [ first2 ] if-empty ;
: 2seq>seq ( seq1 seq2 -- seq )
#! { aceg } { bdfh } -> { abcdefgh }
[ zip concat ] keep like ;
:: interleaved-checksum ( bytes checksum -- seq )
bytes [ zero? ] trim-head
dup length odd? [ rest-slice ] when
seq>2seq [ checksum checksum-bytes ] bi@ 2seq>seq ;