Overview
Comment: | Implement MD5 hash algorithm. (This is not used in Free Hero Mesh, but may be used in other programs e.g. to compute the Z card in Fossil decks.) |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
2e44cdb36f2293ddedbac013e5ede2d2 |
User & Date: | user on 2022-06-13 23:57:13 |
Other Links: | manifest | tags |
Context
2022-06-15
| ||
21:33 | In the OBJECTS virtual table, efficiently check if you are trying to find only bizarro objects in the normal world or vice versa. check-in: 4d77f3e034 user: user tags: trunk | |
2022-06-13
| ||
23:57 | Implement MD5 hash algorithm. (This is not used in Free Hero Mesh, but may be used in other programs e.g. to compute the Z card in Fossil decks.) check-in: 2e44cdb36f user: user tags: trunk | |
23:08 | Split out the code for making a new puzzle set into a separate subroutine, and do not add user cache entries for them until they are loaded the first time. check-in: a36346d28e user: user tags: trunk | |
Changes
Modified README from [35078ecb98] to [4be476e41d].
︙ | ︙ | |||
156 157 158 159 160 161 162 163 164 165 166 167 168 169 | * codepage.har: Contains fonts for code pages. Code page 437 is included inside of the executable file and does not need this file; this file is only needed for code pages other than 437. * default.heromeshrc: An example configuration file which you can customize for your own use. You should at least set the proper paths to the files. * imgtofhm.c: Converts pictures into the Free Hero Mesh format. The command line arguments are: the maximum number of pictures, the lump name prefix, and then the transparency colour (in hex format) (optional). It receives the pictures in farbfeld format in a vertical strip from stdin, and writes the Hamster archive to stdout (which can be appended to a .xclass file). | > > > > > > | 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 | * codepage.har: Contains fonts for code pages. Code page 437 is included inside of the executable file and does not need this file; this file is only needed for code pages other than 437. * default.heromeshrc: An example configuration file which you can customize for your own use. You should at least set the proper paths to the files. * hash.c and hash.h: A library for cryptographic hash calculation. This is compiled into Free Hero Mesh, but as of this writing is not used. It can also be used in other programs independently fo Free Hero Mesh. (Note that some algorithms it implements are considered to be insecure, but are included for compatibility with formats/protocols that use them.) * imgtofhm.c: Converts pictures into the Free Hero Mesh format. The command line arguments are: the maximum number of pictures, the lump name prefix, and then the transparency colour (in hex format) (optional). It receives the pictures in farbfeld format in a vertical strip from stdin, and writes the Hamster archive to stdout (which can be appended to a .xclass file). |
︙ | ︙ |
Modified hash.c from [56ec27ebd5] to [5ced4d6739].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | #if 0 gcc ${CFLAGS:--s -O2} -c -fwrapv hash.c exit #endif /* Some of the code in this file is based on some code from SQLite. The original code and this code also is public domain. */ #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h> #include "hash.h" // ######## SHA-1 hash | > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | #if 0 gcc ${CFLAGS:--s -O2} -c -fwrapv hash.c exit #endif /* Some of the code in this file is based on some code from SQLite. The original code and this code also is public domain. */ #define _GNU_SOURCE #include <stdint.h> #include <stdio.h> #include <stdlib.h> #include <string.h> #include "hash.h" // ######## SHA-1 hash |
︙ | ︙ | |||
631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 | SHA3Update(p, &c3, 1); } for(i=0; i<p->nRate; i++){ p->u.x[i+p->nRate] = p->u.x[i^p->ixMask]; } return &p->u.x[p->nRate]; } // ######## typedef struct { union { SHA1Context sha1; SHA3Context sha3; }; long long alg; FILE*echo; unsigned char*out; } HashState; static ssize_t hash_write(void *cookie, const char *buf, size_t size) { HashState*hs=cookie; if(!size) return 0; if(hs->echo) fwrite(buf,1,size,hs->echo); | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | > > > > | > > > > | | | > > > | > > > > | 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 | SHA3Update(p, &c3, 1); } for(i=0; i<p->nRate; i++){ p->u.x[i+p->nRate] = p->u.x[i^p->ixMask]; } return &p->u.x[p->nRate]; } // ######## MD5 typedef struct { uint8_t chunk[64]; uint64_t len; uint32_t a,b,c,d; } MD5Context; static void md5_init(MD5Context*v) { v->len=0; v->a=0x67452301; v->b=0xEFCDAB89; v->c=0x98BADCFE; v->d=0x10325476; } static void md5_step(MD5Context*v) { static const uint8_t s[64]={ 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 7, 12, 17, 22, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 5, 9, 14, 20, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 4, 11, 16, 23, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, 6, 10, 15, 21, }; static const uint32_t k[64]={ 0xD76AA478, 0xE8C7B756, 0x242070DB, 0xC1BDCEEE, 0xF57C0FAF, 0x4787C62A, 0xA8304613, 0xFD469501, 0x698098D8, 0x8B44F7AF, 0xFFFF5BB1, 0x895CD7BE, 0x6B901122, 0xFD987193, 0xA679438E, 0x49B40821, 0xF61E2562, 0xC040B340, 0x265E5A51, 0xE9B6C7AA, 0xD62F105D, 0x02441453, 0xD8A1E681, 0xE7D3FBC8, 0x21E1CDE6, 0xC33707D6, 0xF4D50D87, 0x455A14ED, 0xA9E3E905, 0xFCEFA3F8, 0x676F02D9, 0x8D2A4C8A, 0xFFFA3942, 0x8771F681, 0x6D9D6122, 0xFDE5380C, 0xA4BEEA44, 0x4BDECFA9, 0xF6BB4B60, 0xBEBFBC70, 0x289B7EC6, 0xEAA127FA, 0xD4EF3085, 0x04881D05, 0xD9D4D039, 0xE6DB99E5, 0x1FA27CF8, 0xC4AC5665, 0xF4292244, 0x432AFF97, 0xAB9423A7, 0xFC93A039, 0x655B59C3, 0x8F0CCC92, 0xFFEFF47D, 0x85845DD1, 0x6FA87E4F, 0xFE2CE6E0, 0xA3014314, 0x4E0811A1, 0xF7537E82, 0xBD3AF235, 0x2AD7D2BB, 0xEB86D391, }; static const uint8_t g[64]={ 0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 52, 56, 60, 4, 24, 44, 0, 20, 40, 60, 16, 36, 56, 12, 32, 52, 8, 28, 48, 20, 32, 44, 56, 4, 16, 28, 40, 52, 0, 12, 24, 36, 48, 60, 8, 0, 28, 56, 20, 48, 12, 40, 4, 32, 60, 24, 52, 16, 44, 8, 36, }; uint32_t a,b,c,d,f,i; a=v->a; b=v->b; c=v->c; d=v->d; for(i=0;i<64;i++) { switch(i&0x30) { case 0x00: f=(b&c)|(d&~b); break; case 0x10: f=(b&d)|(c&~d); break; case 0x20: f=b^c^d; break; case 0x30: f=c^(b|~d); break; } f+=a+k[i]+v->chunk[g[i]]+(v->chunk[g[i]+1]<<8)+(v->chunk[g[i]+2]<<16)+(v->chunk[g[i]+3]<<24); a=d; d=c; c=b; b+=(f<<s[i])|(f>>(32-s[i])); } v->a+=a; v->b+=b; v->c+=c; v->d+=d; } static void md5_write(MD5Context*v,const char*buf,size_t len) { size_t n=len; size_t i; while(n) { i=n; if(i>64-(v->len&63)) i=64-(v->len&63); memcpy(v->chunk+(v->len&63),buf,i); buf+=i; v->len+=i; n-=i; if(!(v->len&63)) md5_step(v); } } static void md5_finish(MD5Context*v,unsigned char*o) { uint64_t n=v->len*8; uint8_t buf[8]; buf[0]=n>>000; buf[1]=n>>010; buf[2]=n>>020; buf[3]=n>>030; buf[4]=n>>040; buf[5]=n>>050; buf[6]=n>>060; buf[7]=n>>070; md5_write(v,"\x80",1); memset(v->chunk+(v->len&63),0,64-(v->len&63)); if((v->len&63)>56) { md5_step(v); memset(v->chunk,0,56); } memcpy(v->chunk+56,buf,8); md5_step(v); o[0]=v->a; o[1]=v->a>>8; o[2]=v->a>>16; o[3]=v->a>>24; o[4]=v->b; o[5]=v->b>>8; o[6]=v->b>>16; o[7]=v->b>>24; o[8]=v->c; o[9]=v->c>>8; o[10]=v->c>>16; o[11]=v->c>>24; o[12]=v->d; o[13]=v->d>>8; o[14]=v->d>>16; o[15]=v->d>>24; } // ######## typedef struct { union { SHA1Context sha1; SHA3Context sha3; MD5Context md5; }; long long alg; FILE*echo; unsigned char*out; } HashState; static ssize_t hash_write(void *cookie, const char *buf, size_t size) { HashState*hs=cookie; if(!size) return 0; if(hs->echo) fwrite(buf,1,size,hs->echo); switch(hs->alg) { case HASH_SHA1: sha1_hash_step(&hs->sha1,buf,size); break; case HASH_SHA3_224: case HASH_SHA3_256: case HASH_SHA3_384: case HASH_SHA3_512: SHA3Update(&hs->sha3,buf,size); break; case HASH_MD5: md5_write(&hs->md5,buf,size); break; } return size; } static int hash_close(void *cookie) { HashState*hs=cookie; switch(hs->alg) { case HASH_SHA1: sha1_hash_finish(&hs->sha1,hs->out); break; case HASH_SHA3_224: case HASH_SHA3_256: case HASH_SHA3_384: case HASH_SHA3_512: memcpy(hs->out,SHA3Final(&hs->sha3),hash_length(hs->alg)); break; case HASH_MD5: md5_finish(&hs->md5,hs->out); break; } free(cookie); return 0; } long hash_length(long long alg) { switch(alg) { case HASH_SHA1: return 20; case HASH_SHA3_224: return 224/8; case HASH_SHA3_256: return 256/8; case HASH_SHA3_384: return 384/8; case HASH_SHA3_512: return 512/8; case HASH_MD5: return 16; default: return 0; } } FILE*hash_stream(long long alg,FILE*echo,unsigned char*out) { HashState*hs=malloc(sizeof(HashState)); FILE*fp; if(!hs) return 0; switch(alg) { case HASH_SHA1: sha1_hash_init(&hs->sha1); break; case HASH_SHA3_224: SHA3Init(&hs->sha3,224); break; case HASH_SHA3_256: SHA3Init(&hs->sha3,256); break; case HASH_SHA3_384: SHA3Init(&hs->sha3,384); break; case HASH_SHA3_512: SHA3Init(&hs->sha3,512); break; case HASH_MD5: md5_init(&hs->md5); break; default: free(hs); return 0; } fp=fopencookie(hs,"w",(cookie_io_functions_t){.write=hash_write,.close=hash_close}); if(!fp) { free(hs); return 0; } |
︙ | ︙ |
Modified hash.h from [ed7b8f308a] to [42028383bc].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | // Free public domain cryptographic hash library #define HASH_SHA1 0x11 #define HASH_SHA3_512 0x14 #define HASH_SHA3_384 0x15 #define HASH_SHA3_256 0x16 #define HASH_SHA3_224 0x17 long hash_length(long long alg); // Tell the length (in bytes) of the hash of the specified algorithm. If // it is not implemented, then the result is zero. FILE*hash_stream(long long alg,FILE*echo,unsigned char*out); // Returns a writable stream. If the echo stream is not null, then any | > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | // Free public domain cryptographic hash library #define HASH_SHA1 0x11 #define HASH_SHA3_512 0x14 #define HASH_SHA3_384 0x15 #define HASH_SHA3_256 0x16 #define HASH_SHA3_224 0x17 #define HASH_MD5 0xD5 long hash_length(long long alg); // Tell the length (in bytes) of the hash of the specified algorithm. If // it is not implemented, then the result is zero. FILE*hash_stream(long long alg,FILE*echo,unsigned char*out); // Returns a writable stream. If the echo stream is not null, then any |
︙ | ︙ |