Adding the "internet checksum" (RFC 1071).

db4
John Benediktsson 2010-08-13 14:32:26 -07:00
parent 6c4ba923ae
commit c3045005ef
4 changed files with 40 additions and 0 deletions

View File

@ -0,0 +1 @@
John Benediktsson

View File

@ -0,0 +1,22 @@
! Copyright (C) 2010 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: checksums checksums.internet tools.test ;
IN: checksums
[ B{ 255 255 } ] [ { } internet checksum-bytes ] unit-test
[ B{ 254 255 } ] [ { 1 } internet checksum-bytes ] unit-test
[ B{ 254 253 } ] [ { 1 2 } internet checksum-bytes ] unit-test
[ B{ 251 253 } ] [ { 1 2 3 } internet checksum-bytes ] unit-test
: test-data ( -- bytes )
B{
HEX: 00 HEX: 01
HEX: f2 HEX: 03
HEX: f4 HEX: f5
HEX: f6 HEX: f7
} ;
[ B{ 34 13 } ] [ test-data internet checksum-bytes ] unit-test

View File

@ -0,0 +1,16 @@
! Copyright (C) 2010 John Benediktsson
! See http://factorcode.org/license.txt for BSD license
USING: checksums grouping io.binary kernel math sequences ;
IN: checksums.internet
SINGLETON: internet ! RFC 1071
INSTANCE: internet checksum
M: internet checksum-bytes
drop 0 swap 2 <sliced-groups> [ le> + ] each
[ -16 shift ] [ HEX: ffff bitand ] bi +
[ -16 shift ] keep + bitnot 2 >le ;

View File

@ -0,0 +1 @@
Internet (RFC 1071) checksum algorithm