This adds SHA1 support and turns the MD5 package into the Digest
package. The old name is still usable. Paolo 2007-07-17 Paolo Bonzini <[hidden email]> * lib-src/sha1.c: New. * lib-src/sha1.h: New. * packages/digest/package.xml: Add new files, rename module. * packages/digest/digest.st: New. * packages/digest/md5.st: Adjust for new MessageDigest superclass. * packages/digest/sha1.st: New. * packages/digest/md5.c: Rename to... * packages/digest/digest.c: ... this. Add SHA1 wrappers. * packages/digest/md5tests.st: Rename to... * packages/digest/mdtests.st: ... this. Add SHA1 tests. * looking for [hidden email]--2004b/smalltalk--devo--2.2--patch-475 to compare with * comparing to [hidden email]--2004b/smalltalk--devo--2.2--patch-475 A lib-src/sha1.c A lib-src/sha1.h A packages/digest/ChangeLog A packages/digest/digest.st A packages/digest/sha1.st M packages/digest/mdtests.st M lib-src/ChangeLog M tests/testsuite.at M tests/testsuite M packages/digest/Makefile.am M packages/digest/package.xml M configure.ac M packages/digest/digest.c M packages/digest/md5.st M lib-src/Makefile.am M packages.xml /> packages/md5 packages/digest => packages/md5/md5.c packages/digest/digest.c => packages/md5/md5tests.st packages/digest/mdtests.st * modified files --- orig/configure.ac +++ mod/configure.ac @@ -294,7 +294,7 @@ GST_PACKAGE([Iconv], [iconv], [am_cv_func_iconv], [Makefile], [iconv.la]) GST_PACKAGE([Java], [java]) -GST_PACKAGE([MD5], [md5], [], [], [Makefile], [md5.la]) +GST_PACKAGE([Digest], [digest], [], [], [Makefile], [digest.la]) GST_PACKAGE([MySQL], [mysql]) GST_PACKAGE([NCurses], [ncurses], --- orig/lib-src/Makefile.am +++ mod/lib-src/Makefile.am @@ -5,11 +5,11 @@ library_la_DEPENDENCIES = @LTLIBOBJS@ @ # These are to be included *always*, the rest are driven by configure.in library_la_SOURCES = \ - qsort.c getopt.c getopt1.c md5.c avltrees.c rbtrees.c \ + qsort.c getopt.c getopt1.c md5.c sha1.c avltrees.c rbtrees.c \ strspell.c signalx.c ltdl.c regex.c socketx.c noinst_HEADERS = \ - obstack.h poll_.h getopt.h md5.h ansidecl.h strspell.h \ + obstack.h poll_.h getopt.h md5.h sha1.h ansidecl.h strspell.h \ mathl.h trigl.h avltrees.h rbtrees.h trigl.c sincosl.c \ signalx.h socketx.h ltdl.h regex.h --- orig/packages.xml +++ mod/packages.xml @@ -1,5 +1,10 @@ <packages> <package> + <name>MD5</name> + <prereq>Digest</prereq> +</package> + +<package> <name>Regex</name> </package> --- orig/packages/md5/Makefile.am +++ mod/packages/digest/Makefile.am @@ -1,9 +1,9 @@ gst_module_ldflags = -rpath $(pkglibdir) -release $(VERSION) -module \ -no-undefined -export-symbols-regex gst_initModule -pkglib_LTLIBRARIES = md5.la -md5_la_SOURCES = md5.c -md5_la_LIBADD = ../../lib-src/library.la -md5_la_LDFLAGS = $(gst_module_ldflags) +pkglib_LTLIBRARIES = digest.la +digest_la_SOURCES = digest.c +digest_la_LIBADD = ../../lib-src/library.la +digest_la_LDFLAGS = $(gst_module_ldflags) AM_CPPFLAGS = -I$(top_srcdir)/libgst -I$(top_srcdir)/lib-src --- orig/packages/md5/md5.c +++ mod/packages/digest/digest.c @@ -1,13 +1,13 @@ /*********************************************************************** * - * MD5 interface definitions for GNU Smalltalk + * Digest module definitions for GNU Smalltalk * * ***********************************************************************/ /*********************************************************************** * - * Copyright 2002, 2006 Free Software Foundation, Inc. + * Copyright 2005, 2007 Free Software Foundation, Inc. * Written by Paolo Bonzini. * * This file is part of GNU Smalltalk. @@ -57,11 +57,12 @@ #endif #include "md5.h" +#include "sha1.h" #include "gstpub.h" static VMProxy *vmProxy; -static OOP MD5AllocOOP (void); +static OOP SHA1AllocOOP (void); OOP MD5AllocOOP (void) @@ -71,6 +72,14 @@ MD5AllocOOP (void) return vmProxy->byteArrayToOOP ((char *) &ctx, sizeof (ctx)); } +OOP +SHA1AllocOOP (void) +{ + struct sha1_ctx ctx; + sha1_init_ctx (&ctx); + return vmProxy->byteArrayToOOP ((char *) &ctx, sizeof (ctx)); +} + void gst_initModule (VMProxy * proxy) { @@ -78,4 +87,8 @@ gst_initModule (VMProxy * proxy) vmProxy->defineCFunc ("MD5AllocOOP", MD5AllocOOP); vmProxy->defineCFunc ("MD5Update", md5_process_bytes); vmProxy->defineCFunc ("MD5Final", md5_finish_ctx); + + vmProxy->defineCFunc ("SHA1AllocOOP", SHA1AllocOOP); + vmProxy->defineCFunc ("SHA1Update", sha1_process_bytes); + vmProxy->defineCFunc ("SHA1Final", sha1_finish_ctx); } --- orig/packages/md5/md5.st +++ mod/packages/digest/md5.st @@ -1,6 +1,6 @@ "====================================================================== | -| MD5 module declarations +| MD5 class declarations | | ======================================================================" @@ -8,7 +8,7 @@ "====================================================================== | -| Copyright 2001, 2005 Free Software Foundation, Inc. +| Copyright 2001, 2005, 2007 Free Software Foundation, Inc. | Written by Paolo Bonzini | | This file is part of the GNU Smalltalk class library. @@ -30,12 +30,18 @@ | ======================================================================" -Object subclass: #MD5 - instanceVariableNames: 'state' +MessageDigest subclass: #MD5 + instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Examples-Modules'! +!MD5 class methodsFor: 'C call-outs'! + +new + ^self basicNew initialize +! ! + !MD5 methodsFor: 'C call-outs'! newState @@ -50,40 +56,16 @@ finalize: state in: digest <cCall: 'MD5Final' returning: #void args: #(#byteArray #byteArrayOut)> ! ! -!MD5 class methodsFor: 'instance creation'! - -new - ^self basicNew initialize -! - -new: initialString - ^self basicNew initialize; nextPutAll: initialString; yourself -! ! - -!MD5 class methodsFor: 'checksumming'! - -digestOf: aStringOrStream - ^(self new: aStringOrStream) digest -! - -hexDigestOf: aStringOrStream - ^(self new: aStringOrStream) hexDigest -! - !MD5 methodsFor: 'initialization'! initialize - state := self newState + self state: self newState ! ! !MD5 methodsFor: 'checksumming'! -copy - ^self deepCopy -! - nextPut: char - self combine: (String with: char) size: 1 into: state + self combine: (String with: char) size: 1 into: self state ! nextPutAll: aStringOrStream @@ -93,7 +75,7 @@ nextPutAll: aStringOrStream self combine: aStringOrStream size: aStringOrStream size - into: state + into: self state ] ifFalse: [ buffer := aStringOrStream species new: 1024. @@ -102,46 +84,17 @@ nextPutAll: aStringOrStream n := n + 1. buffer at: n put: each. n = 1024 ifTrue: [ - self combine: buffer size: n into: state. + self combine: buffer size: n into: self state. n := 0 ] ]. - self combine: buffer size: n into: state. + self combine: buffer size: n into: self state. ] ! -partialDigest - ^self digestFor: state copy -! - digest - ^self digestFor: state -! - -partialHexDigest - ^self hexDigestFor: state copy -! - -hexDigest - ^self hexDigestFor: state -! ! - -!MD5 methodsFor: 'private'! - -digestFor: aState | answer | answer := ByteArray new: 16. - self finalize: aState in: answer. + self finalize: self state in: answer. ^answer -! - -hexDigestFor: aState - | digest answer | - digest := self digestFor: aState. - answer := String new: 32. - digest keysAndValuesDo: [ :i :each | - answer at: i + i - 1 put: (Character digitValue: each // 16). - answer at: i + i put: (Character digitValue: each \\ 16) - ]. - ^answer asLowercase ! ! --- orig/packages/md5/md5tests.st +++ mod/packages/digest/mdtests.st @@ -1,6 +1,6 @@ "====================================================================== | -| MD5 tests declarations +| Message digest tests declarations | | ======================================================================" @@ -28,54 +28,59 @@ | ======================================================================" -TestCase subclass: #MD5Test +TestCase subclass: #MessageDigestTest instanceVariableNames: '' classVariableNames: '' poolDictionaries: '' category: 'Examples-Modules'! -!MD5Test methodsFor: 'test vectors'! +!MessageDigestTest methodsFor: 'test vectors'! nullDigest - ^#[16rD4 16r1D 16r8C 16rD9 16r8F 16r00 16rB2 16r04 - 16rE9 16r80 16r09 16r98 16rEC 16rF8 16r42 16r7E]! + ^self hexToByteArray: self hexNullDigest! hexNullDigest - ^'d41d8cd98f00b204e9800998ecf8427e'! + self subclassResponsibility! abcDigest - ^#[16r90 16r01 16r50 16r98 16r3C 16rD2 16r4F 16rB0 - 16rD6 16r96 16r3F 16r7D 16r28 16rE1 16r7F 16r72]! + ^self hexToByteArray: self hexAbcDigest! hexAbcDigest - ^'900150983cd24fb0d6963f7d28e17f72'! + self subclassResponsibility! abcdefDigest - ^#[16rE8 16r0B 16r50 16r17 16r09 16r89 16r50 16rFC - 16r58 16rAA 16rD8 16r3C 16r8C 16r14 16r97 16r8E]! + ^self hexToByteArray: self hexAbcdefDigest! hexAbcdefDigest - ^'e80b5017098950fc58aad83c8c14978e'! + self subclassResponsibility! size64 ^(2 to: 37) inject: '' into: [ :a :b | a, b printString ]! size64Digest - ^#[16r16 16r5B 16r2B 16r14 16rEC 16rCD 16rE0 16r3D - 16rE4 16r74 16r2A 16r2F 16r93 16r90 16rE1 16rA1]! + ^self hexToByteArray: self hexSize64Digest! hexSize64Digest - ^'165b2b14eccde03de4742a2f9390e1a1'! + self subclassResponsibility! size128 ^(2 to: 69) inject: '' into: [ :a :b | a, b printString ]! size128Digest - ^#[16r59 16rBD 16rA0 16r9A 16r8B 16r3E 16r1D 16r18 - 16r62 16r37 16rED 16r0F 16rED 16r34 16rD8 16r7A]! + ^self hexToByteArray: self hexSize128Digest! hexSize128Digest - ^'59bda09a8b3e1d186237ed0fed34d87a'! + self subclassResponsibility! + +hexToByteArray: hex + | ba | + ba := ByteArray new: hex size / 2. + 1 to: hex size by: 2 do: [ :i | + ba + at: i // 2 + 1 + put: (hex at: i) asUppercase digitValue * 16 + + (hex at: i + 1) asUppercase digitValue ]. + ^ba! allTestCases ^{ '' -> self nullDigest. @@ -91,37 +96,37 @@ allHexTestCases self size64 -> self hexSize64Digest. self size128 -> self hexSize128Digest }! -!MD5Test methodsFor: 'testing'! +!MessageDigestTest methodsFor: 'testing'! testDigestOf self allTestCases do: [ :each | - self assert: (MD5 digestOf: each key) = each value ]! + self assert: (self digestClass digestOf: each key) = each value ]! testByteArray self allTestCases do: [ :each | - self assert: (MD5 digestOf: each key asByteArray) = each value ]! + self assert: (self digestClass digestOf: each key asByteArray) = each value ]! testHexDigestOf self allHexTestCases do: [ :each | - self assert: (MD5 hexDigestOf: each key) = each value ]! + self assert: (self digestClass hexDigestOf: each key) = each value ]! testNextPut self allTestCases do: [ :each | | md5 | - md5 := MD5 new. + md5 := self digestClass new. each key do: [ :ch | md5 nextPut: ch ]. self assert: md5 digest = each value ]! testNextPutAll self allTestCases do: [ :each | | md5 | - md5 := MD5 new. + md5 := self digestClass new. md5 nextPutAll: each key readStream. self assert: md5 digest = each value ]! testPartial | md5 | - md5 := MD5 new. + md5 := self digestClass new. md5 nextPutAll: 'abc'. self assert: md5 partialDigest = self abcDigest. md5 nextPutAll: 'def'. @@ -130,9 +135,61 @@ testPartial testPartialHex | md5 | - md5 := MD5 new. + md5 := self digestClass new. md5 nextPutAll: 'abc'. self assert: md5 partialHexDigest = self hexAbcDigest. md5 nextPutAll: 'def'. self assert: md5 partialHexDigest = self hexAbcdefDigest. self assert: md5 hexDigest = self hexAbcdefDigest! ! + +MessageDigestTest subclass: #MD5Test + instanceVariableNames: '' + classVariableNames: '' + poolDictionaries: '' + category: 'Examples-Modules'! + +!MD5Test methodsFor: 'test vectors'! + +hexNullDigest + ^'d41d8cd98f00b204e9800998ecf8427e'! + +hexAbcDigest + ^'900150983cd24fb0d6963f7d28e17f72'! + +hexAbcdefDigest + ^'e80b5017098950fc58aad83c8c14978e'! + +hexSize64Digest + ^'165b2b14eccde03de4742a2f9390e1a1'! + +hexSize128Digest + ^'59bda09a8b3e1d186237ed0fed34d87a'! + +digestClass + ^MD5! ! + +MessageDigestTest subclass: #SHA1Test + instanceVariableNames: '' + classVariableNames: '' + poolDictionaries: '' + category: 'Examples-Modules'! + +!SHA1Test methodsFor: 'test vectors'! + +hexNullDigest + ^'da39a3ee5e6b4b0d3255bfef95601890afd80709'! + +hexAbcDigest + ^'a9993e364706816aba3e25717850c26c9cd0d89d'! + +hexAbcdefDigest + ^'1f8ac10f23c5b5bc1167bda84b833e5c057a77d2'! + +hexSize64Digest + ^'6a90ea3e17064652ed5406d3e10eb9ac2ee9a21e'! + +hexSize128Digest + ^'e02f6ccdd12ebf0958e18aea9fed8fbe818a223c'! + +digestClass + ^SHA1! ! --- orig/packages/md5/package.xml +++ mod/packages/digest/package.xml @@ -1,13 +1,17 @@ <package> - <name>MD5</name> + <name>Digest</name> + <filein>digest.st</filein> <filein>md5.st</filein> - <module>md5</module> + <filein>sha1.st</filein> + <module>digest</module> <test> - <sunit>MD5Test</sunit> - <filein>md5tests.st</filein> + <sunit>MD5Test SHA1Test</sunit> + <filein>mdtests.st</filein> </test> + <file>digest.st</file> <file>md5.st</file> - <file>md5tests.st</file> + <file>sha1.st</file> + <file>mdtests.st</file> </package> --- orig/tests/testsuite +++ mod/tests/testsuite @@ -736,9 +736,9 @@ at_help_all="1;testsuite.at:27;arrays.st 108;testsuite.at:142;Continuations;Continuations SUnit; 109;testsuite.at:143;DebugTools;DebugTools SUnit; 110;testsuite.at:144;DhbNumericalMethods;DhbNumericalMethods SUnit; -111;testsuite.at:145;GDBM;GDBM SUnit; -112;testsuite.at:146;Iconv;Iconv SUnit; -113;testsuite.at:147;MD5;MD5 SUnit; +111;testsuite.at:145;Digest;Digest SUnit; +112;testsuite.at:146;GDBM;GDBM SUnit; +113;testsuite.at:147;Iconv;Iconv SUnit; 114;testsuite.at:148;ZLib;ZLib SUnit; " @@ -8660,10 +8660,10 @@ $at_traceon at_status=`cat "$at_status_file"` ;; - 111 ) # 111. testsuite.at:145: GDBM + 111 ) # 111. testsuite.at:145: Digest at_setup_line='testsuite.at:145' - at_desc="GDBM" - $at_quiet $ECHO_N "111: GDBM $ECHO_C" + at_desc="Digest" + $at_quiet $ECHO_N "111: Digest $ECHO_C" at_xfail=no echo "# -*- compilation -*-" >> "$at_group_log" ( @@ -8679,36 +8679,25 @@ $at_traceon esac $at_traceoff -echo "$at_srcdir/testsuite.at:145: { (cd \$abs_top_builddir && gst \$image_path -f \$abs_top_srcdir/scripts/Test.st -p GDBM - ret=\$? - case \$ret in - 2) exit 77 ;; - 0|1) exit \$ret ;; - esac); echo exit \$? > retcode; } | tr -d '\\r'; . retcode" +echo "$at_srcdir/testsuite.at:145: { (cd \$abs_top_builddir && gst \$image_path -f \$abs_top_srcdir/scripts/Test.st -p Digest); echo exit \$? > retcode; } | tr -d '\\r'; . retcode" echo testsuite.at:145 >"$at_check_line_file" at_trace_this= if test -n "$at_traceon"; then - echo 'Not enabling shell tracing (command contains an embedded newline)' + case "{ (cd $abs_top_builddir && gst $image_path -f $abs_top_srcdir/scripts/Test.st -p Digest); echo exit $? > retcode; } | tr -d '\\r'; . retcode" in + *' +'*) echo 'Not enabling shell tracing (command contains an embedded newline)' ;; + *) at_trace_this=yes ;; + esac fi if test -n "$at_trace_this"; then - ( $at_traceon; { (cd $abs_top_builddir && gst $image_path -f $abs_top_srcdir/scripts/Test.st -p GDBM - ret=$? - case $ret in - 2) exit 77 ;; - 0|1) exit $ret ;; - esac); echo exit $? > retcode; } | tr -d '\r'; . retcode ) >"$at_stdout" 2>"$at_stder1" + ( $at_traceon; { (cd $abs_top_builddir && gst $image_path -f $abs_top_srcdir/scripts/Test.st -p Digest); echo exit $? > retcode; } | tr -d '\r'; . retcode ) >"$at_stdout" 2>"$at_stder1" at_status=$? grep '^ *+' "$at_stder1" >&2 grep -v '^ *+' "$at_stder1" >"$at_stderr" else - ( :; { (cd $abs_top_builddir && gst $image_path -f $abs_top_srcdir/scripts/Test.st -p GDBM - ret=$? - case $ret in - 2) exit 77 ;; - 0|1) exit $ret ;; - esac); echo exit $? > retcode; } | tr -d '\r'; . retcode ) >"$at_stdout" 2>"$at_stderr" + ( :; { (cd $abs_top_builddir && gst $image_path -f $abs_top_srcdir/scripts/Test.st -p Digest); echo exit $? > retcode; } | tr -d '\r'; . retcode ) >"$at_stdout" 2>"$at_stderr" at_status=$? fi @@ -8737,10 +8726,10 @@ $at_traceon at_status=`cat "$at_status_file"` ;; - 112 ) # 112. testsuite.at:146: Iconv + 112 ) # 112. testsuite.at:146: GDBM at_setup_line='testsuite.at:146' - at_desc="Iconv" - $at_quiet $ECHO_N "112: Iconv $ECHO_C" + at_desc="GDBM" + $at_quiet $ECHO_N "112: GDBM $ECHO_C" at_xfail=no echo "# -*- compilation -*-" >> "$at_group_log" ( @@ -8756,7 +8745,7 @@ $at_traceon esac $at_traceoff -echo "$at_srcdir/testsuite.at:146: { (cd \$abs_top_builddir && gst \$image_path -f \$abs_top_srcdir/scripts/Test.st -p Iconv +echo "$at_srcdir/testsuite.at:146: { (cd \$abs_top_builddir && gst \$image_path -f \$abs_top_srcdir/scripts/Test.st -p GDBM ret=\$? case \$ret in 2) exit 77 ;; @@ -8770,7 +8759,7 @@ if test -n "$at_traceon"; then fi if test -n "$at_trace_this"; then - ( $at_traceon; { (cd $abs_top_builddir && gst $image_path -f $abs_top_srcdir/scripts/Test.st -p Iconv + ( $at_traceon; { (cd $abs_top_builddir && gst $image_path -f $abs_top_srcdir/scripts/Test.st -p GDBM ret=$? case $ret in 2) exit 77 ;; @@ -8780,7 +8769,7 @@ if test -n "$at_trace_this"; then grep '^ *+' "$at_stder1" >&2 grep -v '^ *+' "$at_stder1" >"$at_stderr" else - ( :; { (cd $abs_top_builddir && gst $image_path -f $abs_top_srcdir/scripts/Test.st -p Iconv + ( :; { (cd $abs_top_builddir && gst $image_path -f $abs_top_srcdir/scripts/Test.st -p GDBM ret=$? case $ret in 2) exit 77 ;; @@ -8814,10 +8803,10 @@ $at_traceon at_status=`cat "$at_status_file"` ;; - 113 ) # 113. testsuite.at:147: MD5 + 113 ) # 113. testsuite.at:147: Iconv at_setup_line='testsuite.at:147' - at_desc="MD5" - $at_quiet $ECHO_N "113: MD5 $ECHO_C" + at_desc="Iconv" + $at_quiet $ECHO_N "113: Iconv $ECHO_C" at_xfail=no echo "# -*- compilation -*-" >> "$at_group_log" ( @@ -8833,25 +8822,36 @@ $at_traceon esac $at_traceoff -echo "$at_srcdir/testsuite.at:147: { (cd \$abs_top_builddir && gst \$image_path -f \$abs_top_srcdir/scripts/Test.st -p MD5); echo exit \$? > retcode; } | tr -d '\\r'; . retcode" +echo "$at_srcdir/testsuite.at:147: { (cd \$abs_top_builddir && gst \$image_path -f \$abs_top_srcdir/scripts/Test.st -p Iconv + ret=\$? + case \$ret in + 2) exit 77 ;; + 0|1) exit \$ret ;; + esac); echo exit \$? > retcode; } | tr -d '\\r'; . retcode" echo testsuite.at:147 >"$at_check_line_file" at_trace_this= if test -n "$at_traceon"; then - case "{ (cd $abs_top_builddir && gst $image_path -f $abs_top_srcdir/scripts/Test.st -p MD5); echo exit $? > retcode; } | tr -d '\\r'; . retcode" in - *' -'*) echo 'Not enabling shell tracing (command contains an embedded newline)' ;; - *) at_trace_this=yes ;; - esac + echo 'Not enabling shell tracing (command contains an embedded newline)' fi if test -n "$at_trace_this"; then - ( $at_traceon; { (cd $abs_top_builddir && gst $image_path -f $abs_top_srcdir/scripts/Test.st -p MD5); echo exit $? > retcode; } | tr -d '\r'; . retcode ) >"$at_stdout" 2>"$at_stder1" + ( $at_traceon; { (cd $abs_top_builddir && gst $image_path -f $abs_top_srcdir/scripts/Test.st -p Iconv + ret=$? + case $ret in + 2) exit 77 ;; + 0|1) exit $ret ;; + esac); echo exit $? > retcode; } | tr -d '\r'; . retcode ) >"$at_stdout" 2>"$at_stder1" at_status=$? grep '^ *+' "$at_stder1" >&2 grep -v '^ *+' "$at_stder1" >"$at_stderr" else - ( :; { (cd $abs_top_builddir && gst $image_path -f $abs_top_srcdir/scripts/Test.st -p MD5); echo exit $? > retcode; } | tr -d '\r'; . retcode ) >"$at_stdout" 2>"$at_stderr" + ( :; { (cd $abs_top_builddir && gst $image_path -f $abs_top_srcdir/scripts/Test.st -p Iconv + ret=$? + case $ret in + 2) exit 77 ;; + 0|1) exit $ret ;; + esac); echo exit $? > retcode; } | tr -d '\r'; . retcode ) >"$at_stdout" 2>"$at_stderr" at_status=$? fi --- orig/tests/testsuite.at +++ mod/tests/testsuite.at @@ -142,7 +142,7 @@ AT_BANNER([Other packages.]) AT_PACKAGE_TEST([Continuations]) AT_PACKAGE_TEST([DebugTools]) AT_PACKAGE_TEST([DhbNumericalMethods]) +AT_PACKAGE_TEST([Digest]) AT_OPTIONAL_PACKAGE_TEST([GDBM]) AT_OPTIONAL_PACKAGE_TEST([Iconv]) -AT_PACKAGE_TEST([MD5]) AT_OPTIONAL_PACKAGE_TEST([ZLib]) * added files --- /dev/null +++ mod/lib-src/sha1.c @@ -0,0 +1,416 @@ +/* sha1.c - Functions to compute SHA1 message digest of files or + memory blocks according to the NIST specification FIPS-180-1. + + Copyright (C) 2000, 2001, 2003, 2004, 2005, 2006 Free Software + Foundation, Inc. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +/* Written by Scott G. Miller + Credits: + Robert Klep <[hidden email]> -- Expansion function fix +*/ + +#include <config.h> + +#include "sha1.h" + +#include <stddef.h> +#include <string.h> + +#if USE_UNLOCKED_IO +# include "unlocked-io.h" +#endif + +#ifdef WORDS_BIGENDIAN +# define SWAP(n) (n) +#else +# define SWAP(n) \ + (((n) << 24) | (((n) & 0xff00) << 8) | (((n) >> 8) & 0xff00) | ((n) >> 24)) +#endif + +#define BLOCKSIZE 4096 +#if BLOCKSIZE % 64 != 0 +# error "invalid BLOCKSIZE" +#endif + +/* This array contains the bytes used to pad the buffer to the next + 64-byte boundary. (RFC 1321, 3.1: Step 1) */ +static const unsigned char fillbuf[64] = { 0x80, 0 /* , 0, 0, ... */ }; + + +/* Take a pointer to a 160 bit block of data (five 32 bit ints) and + initialize it to the start constants of the SHA1 algorithm. This + must be called before using hash in the call to sha1_hash. */ +void +sha1_init_ctx (struct sha1_ctx *ctx) +{ + ctx->A = 0x67452301; + ctx->B = 0xefcdab89; + ctx->C = 0x98badcfe; + ctx->D = 0x10325476; + ctx->E = 0xc3d2e1f0; + + ctx->total[0] = ctx->total[1] = 0; + ctx->buflen = 0; +} + +/* Put result from CTX in first 20 bytes following RESBUF. The result + must be in little endian byte order. + + IMPORTANT: On some systems it is required that RESBUF is correctly + aligned for a 32-bit value. */ +void * +sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf) +{ + ((uint32_t *) resbuf)[0] = SWAP (ctx->A); + ((uint32_t *) resbuf)[1] = SWAP (ctx->B); + ((uint32_t *) resbuf)[2] = SWAP (ctx->C); + ((uint32_t *) resbuf)[3] = SWAP (ctx->D); + ((uint32_t *) resbuf)[4] = SWAP (ctx->E); + + return resbuf; +} + +/* Process the remaining bytes in the internal buffer and the usual + prolog according to the standard and write the result to RESBUF. + + IMPORTANT: On some systems it is required that RESBUF is correctly + aligned for a 32-bit value. */ +void * +sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf) +{ + /* Take yet unprocessed bytes into account. */ + uint32_t bytes = ctx->buflen; + size_t size = (bytes < 56) ? 64 / 4 : 64 * 2 / 4; + + /* Now count remaining bytes. */ + ctx->total[0] += bytes; + if (ctx->total[0] < bytes) + ++ctx->total[1]; + + /* Put the 64-bit file length in *bits* at the end of the buffer. */ + ctx->buffer[size - 2] = SWAP ((ctx->total[1] << 3) | (ctx->total[0] >> 29)); + ctx->buffer[size - 1] = SWAP (ctx->total[0] << 3); + + memcpy (&((char *) ctx->buffer)[bytes], fillbuf, (size - 2) * 4 - bytes); + + /* Process last bytes. */ + sha1_process_block (ctx->buffer, size * 4, ctx); + + return sha1_read_ctx (ctx, resbuf); +} + +/* Compute SHA1 message digest for bytes read from STREAM. The + resulting message digest number will be written into the 16 bytes + beginning at RESBLOCK. */ +int +sha1_stream (FILE *stream, void *resblock) +{ + struct sha1_ctx ctx; + char buffer[BLOCKSIZE + 72]; + size_t sum; + + /* Initialize the computation context. */ + sha1_init_ctx (&ctx); + + /* Iterate over full file contents. */ + while (1) + { + /* We read the file in blocks of BLOCKSIZE bytes. One call of the + computation function processes the whole buffer so that with the + next round of the loop another block can be read. */ + size_t n; + sum = 0; + + /* Read block. Take care for partial reads. */ + while (1) + { + n = fread (buffer + sum, 1, BLOCKSIZE - sum, stream); + + sum += n; + + if (sum == BLOCKSIZE) + break; + + if (n == 0) + { + /* Check for the error flag IFF N == 0, so that we don't + exit the loop after a partial read due to e.g., EAGAIN + or EWOULDBLOCK. */ + if (ferror (stream)) + return 1; + goto process_partial_block; + } + + /* We've read at least one byte, so ignore errors. But always + check for EOF, since feof may be true even though N > 0. + Otherwise, we could end up calling fread after EOF. */ + if (feof (stream)) + goto process_partial_block; + } + + /* Process buffer with BLOCKSIZE bytes. Note that + BLOCKSIZE % 64 == 0 + */ + sha1_process_block (buffer, BLOCKSIZE, &ctx); + } + + process_partial_block:; + + /* Process any remaining bytes. */ + if (sum > 0) + sha1_process_bytes (buffer, sum, &ctx); + + /* Construct result in desired memory. */ + sha1_finish_ctx (&ctx, resblock); + return 0; +} + +/* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The + result is always in little endian byte order, so that a byte-wise + output yields to the wanted ASCII representation of the message + digest. */ +void * +sha1_buffer (const char *buffer, size_t len, void *resblock) +{ + struct sha1_ctx ctx; + + /* Initialize the computation context. */ + sha1_init_ctx (&ctx); + + /* Process whole buffer but last len % 64 bytes. */ + sha1_process_bytes (buffer, len, &ctx); + + /* Put result in desired memory area. */ + return sha1_finish_ctx (&ctx, resblock); +} + +void +sha1_process_bytes (const void *buffer, size_t len, struct sha1_ctx *ctx) +{ + /* When we already have some bits in our internal buffer concatenate + both inputs first. */ + if (ctx->buflen != 0) + { + size_t left_over = ctx->buflen; + size_t add = 128 - left_over > len ? len : 128 - left_over; + + memcpy (&((char *) ctx->buffer)[left_over], buffer, add); + ctx->buflen += add; + + if (ctx->buflen > 64) + { + sha1_process_block (ctx->buffer, ctx->buflen & ~63, ctx); + + ctx->buflen &= 63; + /* The regions in the following copy operation cannot overlap. */ + memcpy (ctx->buffer, + &((char *) ctx->buffer)[(left_over + add) & ~63], + ctx->buflen); + } + + buffer = (const char *) buffer + add; + len -= add; + } + + /* Process available complete blocks. */ + if (len >= 64) + { +#if !_STRING_ARCH_unaligned +# define alignof(type) offsetof (struct { char c; type x; }, x) +# define UNALIGNED_P(p) (((size_t) p) % alignof (uint32_t) != 0) + if (UNALIGNED_P (buffer)) + while (len > 64) + { + sha1_process_block (memcpy (ctx->buffer, buffer, 64), 64, ctx); + buffer = (const char *) buffer + 64; + len -= 64; + } + else +#endif + { + sha1_process_block (buffer, len & ~63, ctx); + buffer = (const char *) buffer + (len & ~63); + len &= 63; + } + } + + /* Move remaining bytes in internal buffer. */ + if (len > 0) + { + size_t left_over = ctx->buflen; + + memcpy (&((char *) ctx->buffer)[left_over], buffer, len); + left_over += len; + if (left_over >= 64) + { + sha1_process_block (ctx->buffer, 64, ctx); + left_over -= 64; + memcpy (ctx->buffer, &ctx->buffer[16], left_over); + } + ctx->buflen = left_over; + } +} + +/* --- Code below is the primary difference between md5.c and sha1.c --- */ + +/* SHA1 round constants */ +#define K1 0x5a827999 +#define K2 0x6ed9eba1 +#define K3 0x8f1bbcdc +#define K4 0xca62c1d6 + +/* Round functions. Note that F2 is the same as F4. */ +#define F1(B,C,D) ( D ^ ( B & ( C ^ D ) ) ) +#define F2(B,C,D) (B ^ C ^ D) +#define F3(B,C,D) ( ( B & C ) | ( D & ( B | C ) ) ) +#define F4(B,C,D) (B ^ C ^ D) + +/* Process LEN bytes of BUFFER, accumulating context into CTX. + It is assumed that LEN % 64 == 0. + Most of this code comes from GnuPG's cipher/sha1.c. */ + +void +sha1_process_block (const void *buffer, size_t len, struct sha1_ctx *ctx) +{ + const uint32_t *words = buffer; + size_t nwords = len / sizeof (uint32_t); + const uint32_t *endp = words + nwords; + uint32_t x[16]; + uint32_t a = ctx->A; + uint32_t b = ctx->B; + uint32_t c = ctx->C; + uint32_t d = ctx->D; + uint32_t e = ctx->E; + + /* First increment the byte count. RFC 1321 specifies the possible + length of the file up to 2^64 bits. Here we only compute the + number of bytes. Do a double word increment. */ + ctx->total[0] += len; + if (ctx->total[0] < len) + ++ctx->total[1]; + +#define rol(x, n) (((x) << (n)) | ((uint32_t) (x) >> (32 - (n)))) + +#define M(I) ( tm = x[I&0x0f] ^ x[(I-14)&0x0f] \ + ^ x[(I-8)&0x0f] ^ x[(I-3)&0x0f] \ + , (x[I&0x0f] = rol(tm, 1)) ) + +#define R(A,B,C,D,E,F,K,M) do { E += rol( A, 5 ) \ + + F( B, C, D ) \ + + K \ + + M; \ + B = rol( B, 30 ); \ + } while(0) + + while (words < endp) + { + uint32_t tm; + int t; + for (t = 0; t < 16; t++) + { + x[t] = SWAP (*words); + words++; + } + + R( a, b, c, d, e, F1, K1, x[ 0] ); + R( e, a, b, c, d, F1, K1, x[ 1] ); + R( d, e, a, b, c, F1, K1, x[ 2] ); + R( c, d, e, a, b, F1, K1, x[ 3] ); + R( b, c, d, e, a, F1, K1, x[ 4] ); + R( a, b, c, d, e, F1, K1, x[ 5] ); + R( e, a, b, c, d, F1, K1, x[ 6] ); + R( d, e, a, b, c, F1, K1, x[ 7] ); + R( c, d, e, a, b, F1, K1, x[ 8] ); + R( b, c, d, e, a, F1, K1, x[ 9] ); + R( a, b, c, d, e, F1, K1, x[10] ); + R( e, a, b, c, d, F1, K1, x[11] ); + R( d, e, a, b, c, F1, K1, x[12] ); + R( c, d, e, a, b, F1, K1, x[13] ); + R( b, c, d, e, a, F1, K1, x[14] ); + R( a, b, c, d, e, F1, K1, x[15] ); + R( e, a, b, c, d, F1, K1, M(16) ); + R( d, e, a, b, c, F1, K1, M(17) ); + R( c, d, e, a, b, F1, K1, M(18) ); + R( b, c, d, e, a, F1, K1, M(19) ); + R( a, b, c, d, e, F2, K2, M(20) ); + R( e, a, b, c, d, F2, K2, M(21) ); + R( d, e, a, b, c, F2, K2, M(22) ); + R( c, d, e, a, b, F2, K2, M(23) ); + R( b, c, d, e, a, F2, K2, M(24) ); + R( a, b, c, d, e, F2, K2, M(25) ); + R( e, a, b, c, d, F2, K2, M(26) ); + R( d, e, a, b, c, F2, K2, M(27) ); + R( c, d, e, a, b, F2, K2, M(28) ); + R( b, c, d, e, a, F2, K2, M(29) ); + R( a, b, c, d, e, F2, K2, M(30) ); + R( e, a, b, c, d, F2, K2, M(31) ); + R( d, e, a, b, c, F2, K2, M(32) ); + R( c, d, e, a, b, F2, K2, M(33) ); + R( b, c, d, e, a, F2, K2, M(34) ); + R( a, b, c, d, e, F2, K2, M(35) ); + R( e, a, b, c, d, F2, K2, M(36) ); + R( d, e, a, b, c, F2, K2, M(37) ); + R( c, d, e, a, b, F2, K2, M(38) ); + R( b, c, d, e, a, F2, K2, M(39) ); + R( a, b, c, d, e, F3, K3, M(40) ); + R( e, a, b, c, d, F3, K3, M(41) ); + R( d, e, a, b, c, F3, K3, M(42) ); + R( c, d, e, a, b, F3, K3, M(43) ); + R( b, c, d, e, a, F3, K3, M(44) ); + R( a, b, c, d, e, F3, K3, M(45) ); + R( e, a, b, c, d, F3, K3, M(46) ); + R( d, e, a, b, c, F3, K3, M(47) ); + R( c, d, e, a, b, F3, K3, M(48) ); + R( b, c, d, e, a, F3, K3, M(49) ); + R( a, b, c, d, e, F3, K3, M(50) ); + R( e, a, b, c, d, F3, K3, M(51) ); + R( d, e, a, b, c, F3, K3, M(52) ); + R( c, d, e, a, b, F3, K3, M(53) ); + R( b, c, d, e, a, F3, K3, M(54) ); + R( a, b, c, d, e, F3, K3, M(55) ); + R( e, a, b, c, d, F3, K3, M(56) ); + R( d, e, a, b, c, F3, K3, M(57) ); + R( c, d, e, a, b, F3, K3, M(58) ); + R( b, c, d, e, a, F3, K3, M(59) ); + R( a, b, c, d, e, F4, K4, M(60) ); + R( e, a, b, c, d, F4, K4, M(61) ); + R( d, e, a, b, c, F4, K4, M(62) ); + R( c, d, e, a, b, F4, K4, M(63) ); + R( b, c, d, e, a, F4, K4, M(64) ); + R( a, b, c, d, e, F4, K4, M(65) ); + R( e, a, b, c, d, F4, K4, M(66) ); + R( d, e, a, b, c, F4, K4, M(67) ); + R( c, d, e, a, b, F4, K4, M(68) ); + R( b, c, d, e, a, F4, K4, M(69) ); + R( a, b, c, d, e, F4, K4, M(70) ); + R( e, a, b, c, d, F4, K4, M(71) ); + R( d, e, a, b, c, F4, K4, M(72) ); + R( c, d, e, a, b, F4, K4, M(73) ); + R( b, c, d, e, a, F4, K4, M(74) ); + R( a, b, c, d, e, F4, K4, M(75) ); + R( e, a, b, c, d, F4, K4, M(76) ); + R( d, e, a, b, c, F4, K4, M(77) ); + R( c, d, e, a, b, F4, K4, M(78) ); + R( b, c, d, e, a, F4, K4, M(79) ); + + a = ctx->A += a; + b = ctx->B += b; + c = ctx->C += c; + d = ctx->D += d; + e = ctx->E += e; + } +} --- /dev/null +++ mod/lib-src/sha1.h @@ -0,0 +1,87 @@ +/* Declarations of functions and data types used for SHA1 sum + library functions. + Copyright (C) 2000, 2001, 2003, 2005, 2006 Free Software Foundation, Inc. + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 2, or (at your option) any + later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software Foundation, + Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ + +#ifndef SHA1_H +# define SHA1_H 1 + +# include <stdio.h> +# include <stdint.h> + +/* Structure to save state of computation between the single steps. */ +struct sha1_ctx +{ + uint32_t A; + uint32_t B; + uint32_t C; + uint32_t D; + uint32_t E; + + uint32_t total[2]; + uint32_t buflen; + uint32_t buffer[32]; +}; + + +/* Initialize structure containing state of computation. */ +extern void sha1_init_ctx (struct sha1_ctx *ctx); + +/* Starting with the result of former calls of this function (or the + initialization function update the context for the next LEN bytes + starting at BUFFER. + It is necessary that LEN is a multiple of 64!!! */ +extern void sha1_process_block (const void *buffer, size_t len, + struct sha1_ctx *ctx); + +/* Starting with the result of former calls of this function (or the + initialization function update the context for the next LEN bytes + starting at BUFFER. + It is NOT required that LEN is a multiple of 64. */ +extern void sha1_process_bytes (const void *buffer, size_t len, + struct sha1_ctx *ctx); + +/* Process the remaining bytes in the buffer and put result from CTX + in first 20 bytes following RESBUF. The result is always in little + endian byte order, so that a byte-wise output yields to the wanted + ASCII representation of the message digest. + + IMPORTANT: On some systems it is required that RESBUF be correctly + aligned for a 32 bits value. */ +extern void *sha1_finish_ctx (struct sha1_ctx *ctx, void *resbuf); + + +/* Put result from CTX in first 20 bytes following RESBUF. The result is + always in little endian byte order, so that a byte-wise output yields + to the wanted ASCII representation of the message digest. + + IMPORTANT: On some systems it is required that RESBUF is correctly + aligned for a 32 bits value. */ +extern void *sha1_read_ctx (const struct sha1_ctx *ctx, void *resbuf); + + +/* Compute SHA1 message digest for bytes read from STREAM. The + resulting message digest number will be written into the 20 bytes + beginning at RESBLOCK. */ +extern int sha1_stream (FILE *stream, void *resblock); + +/* Compute SHA1 message digest for LEN bytes beginning at BUFFER. The + result is always in little endian byte order, so that a byte-wise + output yields to the wanted ASCII representation of the message + digest. */ +extern void *sha1_buffer (const char *buffer, size_t len, void *resblock); + +#endif --- /dev/null +++ mod/packages/digest/digest.st @@ -0,0 +1,98 @@ +"====================================================================== +| +| MessageDigest abstract class declarations +| +| + ======================================================================" + + +"====================================================================== +| +| Copyright 2001, 2005 Free Software Foundation, Inc. +| Written by Paolo Bonzini +| +| This file is part of the GNU Smalltalk class library. +| +| The GNU Smalltalk class library is free software; you can redistribute it +| and/or modify it under the terms of the GNU Lesser General Public License +| as published by the Free Software Foundation; either version 2.1, or (at +| your option) any later version. +| +| The GNU Smalltalk class library is distributed in the hope that it will be +| useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser +| General Public License for more details. +| +| You should have received a copy of the GNU Lesser General Public License +| along with the GNU Smalltalk class library; see the file COPYING.LIB. +| If not, write to the Free Software Foundation, 59 Temple Place - Suite +| 330, Boston, MA 02110-1301, USA. +| + ======================================================================" + +Object subclass: #MessageDigest + instanceVariableNames: 'state' + classVariableNames: '' + poolDictionaries: '' + category: 'Examples-Modules'! + +!MessageDigest class methodsFor: 'instance creation'! + +new: initialString + ^self new nextPutAll: initialString; yourself +! ! + +!MessageDigest class methodsFor: 'checksumming'! + +digestOf: aStringOrStream + ^(self new: aStringOrStream) digest +! + +hexDigestOf: aStringOrStream + ^(self new: aStringOrStream) hexDigest +! + +!MessageDigest methodsFor: 'checksumming'! + +copy + ^self deepCopy +! + +partialDigest + | s digest | + s := state copy. + digest := self digest. + state := s. + ^digest +! + +digest + self subclassResponsibility +! + +partialHexDigest + | s digest | + s := state copy. + digest := self hexDigest. + state := s. + ^digest +! + +hexDigest + | digest answer | + digest := self digest. + answer := String new: digest size * 2. + digest keysAndValuesDo: [ :i :each | + answer at: i + i - 1 put: (Character digitValue: each // 16). + answer at: i + i put: (Character digitValue: each \\ 16) + ]. + ^answer asLowercase +! ! + +!MessageDigest methodsFor: 'private'! + +state + ^state! + +state: anObject + state := anObject! ! --- /dev/null +++ mod/packages/digest/sha1.st @@ -0,0 +1,100 @@ +"====================================================================== +| +| SHA1 class declarations +| +| + ======================================================================" + + +"====================================================================== +| +| Copyright 2007 Free Software Foundation, Inc. +| Written by Paolo Bonzini +| +| This file is part of the GNU Smalltalk class library. +| +| The GNU Smalltalk class library is free software; you can redistribute it +| and/or modify it under the terms of the GNU Lesser General Public License +| as published by the Free Software Foundation; either version 2.1, or (at +| your option) any later version. +| +| The GNU Smalltalk class library is distributed in the hope that it will be +| useful, but WITHOUT ANY WARRANTY; without even the implied warranty of +| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser +| General Public License for more details. +| +| You should have received a copy of the GNU Lesser General Public License +| along with the GNU Smalltalk class library; see the file COPYING.LIB. +| If not, write to the Free Software Foundation, 59 Temple Place - Suite +| 330, Boston, MA 02110-1301, USA. +| + ======================================================================" + +MessageDigest subclass: #SHA1 + instanceVariableNames: '' + classVariableNames: '' + poolDictionaries: '' + category: 'Examples-Modules'! + +!SHA1 class methodsFor: 'C call-outs'! + +new + ^self basicNew initialize +! ! + +!SHA1 methodsFor: 'C call-outs'! + +newState + <cCall: 'SHA1AllocOOP' returning: #smalltalk args: #()> +! + +combine: input size: len into: context + <cCall: 'SHA1Update' returning: #void args: #(#byteArray #int #byteArrayOut)> +! + +finalize: state in: digest + <cCall: 'SHA1Final' returning: #void args: #(#byteArray #byteArrayOut)> +! ! + +!SHA1 methodsFor: 'initialization'! + +initialize + self state: self newState +! ! + +!SHA1 methodsFor: 'checksumming'! + +nextPut: char + self combine: (String with: char) size: 1 into: self state +! + +nextPutAll: aStringOrStream + | buffer n | + (aStringOrStream isKindOf: String) + ifTrue: [ + self + combine: aStringOrStream + size: aStringOrStream size + into: self state + ] + ifFalse: [ + buffer := aStringOrStream species new: 1024. + n := 0. + aStringOrStream do: [ :each | + n := n + 1. + buffer at: n put: each. + n = 1024 ifTrue: [ + self combine: buffer size: n into: self state. + n := 0 + ] + ]. + self combine: buffer size: n into: self state. + ] +! + +digest + | answer | + answer := ByteArray new: 20. + self finalize: self state in: answer. + ^answer +! ! _______________________________________________ help-smalltalk mailing list [hidden email] http://lists.gnu.org/mailman/listinfo/help-smalltalk |
Free forum by Nabble | Edit this page |