2016 Update

This commit is contained in:
Tina Müller 2016-12-05 22:15:40 +01:00
parent 948b86eafa
commit dcf5d15da3
7965 changed files with 139854 additions and 31002 deletions

View file

@ -1,7 +1,12 @@
Encode a string using an MD5 algorithm. The algorithm can be found on [[wp:Md5#Algorithm|wikipedia]].
;Task:
Encode a string using an MD5 algorithm.   The algorithm can be found on   [[wp:Md5#Algorithm|Wikipedia]].
Optionally, validate your implementation by running all of the test values in [http://tools.ietf.org/html/rfc1321 IETF RFC (1321) for MD5]. Additional the RFC provides more precise information on the algorithm than the Wikipedia article.
{{alertbox|lightgray|'''Warning:''' MD5 has [http://tools.ietf.org/html/rfc6151 known weaknesses], including '''collisions''' and [http://www.win.tue.nl/hashclash/rogue-ca/ forged signatures]. Users may consider a stronger alternative when doing production-grade cryptography, such as SHA-256 (from the SHA-2 family) or the upcoming SHA-3.}}
Optionally, validate your implementation by running all of the test values in   [http://tools.ietf.org/html/rfc1321 IETF RFC (1321)   for MD5].
If the solution on this page is a library solution, see [[MD5/Implementation]] for an implementation from scratch.
Additionally,   RFC 1321   provides more precise information on the algorithm than the Wikipedia article.
{{alertbox|lightgray|'''Warning:'''   MD5 has [http://tools.ietf.org/html/rfc6151 known weaknesses], including '''collisions''' and [http://www.win.tue.nl/hashclash/rogue-ca/ forged signatures].   Users may consider a stronger alternative when doing production-grade cryptography, such as SHA-256 (from the SHA-2 family), or the upcoming SHA-3.}}
If the solution on this page is a library solution, see   [[MD5/Implementation]]   for an implementation from scratch.
<br><br>

94
Task/MD5/Fortran/md5.f Normal file
View file

@ -0,0 +1,94 @@
module md5_m
use kernel32
use advapi32
implicit none
integer, parameter :: MD5LEN = 16
contains
subroutine md5hash(name, hash, dwStatus, filesize)
implicit none
character(*) :: name
integer, parameter :: BUFLEN = 32768
integer(HANDLE) :: hFile, hProv, hHash
integer(DWORD) :: dwStatus, nRead
integer(BOOL) :: status
integer(BYTE) :: buffer(BUFLEN)
integer(BYTE) :: hash(MD5LEN)
integer(UINT64) :: filesize
dwStatus = 0
filesize = 0
hFile = CreateFile(trim(name) // char(0), GENERIC_READ, FILE_SHARE_READ, NULL, &
OPEN_EXISTING, FILE_FLAG_SEQUENTIAL_SCAN, NULL)
if (hFile == INVALID_HANDLE_VALUE) then
dwStatus = GetLastError()
print *, "CreateFile failed."
return
end if
if (CryptAcquireContext(hProv, NULL, NULL, PROV_RSA_FULL, &
CRYPT_VERIFYCONTEXT) == FALSE) then
dwStatus = GetLastError()
print *, "CryptAcquireContext failed."
goto 3
end if
if (CryptCreateHash(hProv, CALG_MD5, 0_ULONG_PTR, 0_DWORD, hHash) == FALSE) then
dwStatus = GetLastError()
print *, "CryptCreateHash failed."
go to 2
end if
do
status = ReadFile(hFile, loc(buffer), BUFLEN, loc(nRead), NULL)
if (status == FALSE .or. nRead == 0) exit
filesize = filesize + nRead
if (CryptHashData(hHash, buffer, nRead, 0) == FALSE) then
dwStatus = GetLastError()
print *, "CryptHashData failed."
go to 1
end if
end do
if (status == FALSE) then
dwStatus = GetLastError()
print *, "ReadFile failed."
go to 1
end if
nRead = MD5LEN
if (CryptGetHashParam(hHash, HP_HASHVAL, hash, nRead, 0) == FALSE) then
dwStatus = GetLastError()
print *, "CryptGetHashParam failed.", status, nRead, dwStatus
end if
1 status = CryptDestroyHash(hHash)
2 status = CryptReleaseContext(hProv, 0)
3 status = CloseHandle(hFile)
end subroutine
end module
program md5
use md5_m
implicit none
integer :: n, m, i, j
character(:), allocatable :: name
integer(DWORD) :: dwStatus
integer(BYTE) :: hash(MD5LEN)
integer(UINT64) :: filesize
n = command_argument_count()
do i = 1, n
call get_command_argument(i, length=m)
allocate(character(m) :: name)
call get_command_argument(i, name)
call md5hash(name, hash, dwStatus, filesize)
if (dwStatus*0 == 0) then
do j = 1, MD5LEN
write(*, "(Z2.2)", advance="NO") hash(j)
end do
write(*, "(' ',A,' (',G0,' bytes)')") name, filesize
end if
deallocate(name)
end do
end program

4
Task/MD5/J/md5-2.j Normal file
View file

@ -0,0 +1,4 @@
require '~addons/ide/qt/qt.ijs'
getmd5=: 'md5'&gethash_jqtide_
getmd5 'The quick brown fox jumped over the lazy dog''s back'
e38ca1d920c4b8b8d3946b2c72f01680

View file

@ -0,0 +1,27 @@
#include <pari/pari.h>
#include <openssl/md5.h>
#define HEX(x) (((x) < 10)? (x)+'0': (x)-10+'a')
/*
* PARI/GP func: MD5 hash
*
* gp code: install("plug_md5", "s", "MD5", "<library path>");
*/
GEN plug_md5(char *text)
{
char md[MD5_DIGEST_LENGTH];
char hash[sizeof(md) * 2 + 1];
int i;
MD5((unsigned char*)text, strlen(text), (unsigned char*)md);
for (i = 0; i < sizeof(md); i++) {
hash[i+i] = HEX((md[i] >> 4) & 0x0f);
hash[i+i+1] = HEX(md[i] & 0x0f);
}
hash[sizeof(md) * 2] = 0;
return strtoGENstr(hash);
}

View file

@ -0,0 +1,3 @@
install("plug_md5", "s", "MD5", "~/libmd5.so");
MD5("The quick brown fox jumped over the lazy dog's back")

View file

@ -1,117 +1,114 @@
/*REXX program tests the MD5 procedure as per the test suite in the */
/*────── IETF RFC (1321) ────── The MD5 Message─Digest Algorithm. April 1992.*/
msg.1 = /*─────MD5 test suite [from above doc].*/
/*REXX program tests the MD5 procedure (below) as per a test suite the IETF RFC (1321).*/
msg.1 = /*─────MD5 test suite [from above doc].*/
msg.2 = 'a'
msg.3 = 'abc'
msg.4 = 'message digest'
msg.5 = 'abcdefghijklmnopqrstuvwxyz'
msg.6 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
msg.7 = 12345678901234567890123456789012345678901234567890123456789012345678901234567890
msg.0 = 7 /* [↑] last value doesn't need quotes.*/
do m=1 for msg.0 /*process each of the seven messages. */
say ' in =' msg.m /*display the in message. */
say 'out =' MD5(msg.m) /* " " out " */
say /* " a blank like for a separator.*/
end /*m*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────MD5 subroutine────────────────────────────*/
MD5: procedure; parse arg !; numeric digits 20 /*insure enough decimal digs.*/
parse value '67452301'x 'efcdab89'x '98badcfe'x '10325476'x with a b c d
#=length(!) /*length of the input message*/
L=#*8 // 512; if L<448 then plus=448-L
if L>448 then plus=960-L
if L=448 then plus=512
/* [↓] a little of this, ··· */
$=!'80'x || copies("0"x,plus%8-1)reverse(right(d2c(8*#),4,'0'x)) || "00000000"x
/* [↑] ··· and a little of that.*/
do j=0 to length($)%64-1 /*process the message (lots of steps).*/
a_=a; b_=b; c_=c; d_=d /*save the original values for later.*/
chunk=j*64 /*calculate the size of the chunks. */
do k=1 for 16 /*process the message in chunks. */
!.k=reverse(substr($,chunk+1+4*(k-1),4)) /*magic stuff.*/
end /*k*/
msg.0 = 7 /* [↑] last value doesn't need quotes.*/
do m=1 for msg.0; say /*process each of the seven messages. */
say ' in =' msg.m /*display the in message. */
say 'out =' MD5(msg.m) /* " " out " */
end /*m*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
MD5: procedure; parse arg !; numeric digits 20 /*insure there's enough decimal digits.*/
a='67452301'x; b="efcdab89"x; c='98badcfe'x; d="10325476"x; x00='0'x; x80="80"x
#=length(!) /*length in bytes of the input message.*/
L=#*8//512; if L<448 then plus=448 - L /*is the length less than 448 ? */
if L>448 then plus=960 - L /* " " " greater " " */
if L=448 then plus=512 /* " " " equal to " */
/* [↓] a little of this, ··· */
$=! || x80 || copies(x00, plus%8 -1)reverse(right(d2c(8 * #), 4, x00)) || '00000000'x
/* [↑] ··· and a little of that.*/
do j=0 to length($) % 64 - 1 /*process the message (lots of steps).*/
a_=a; b_=b; c_=c; d_=d /*save the original values for later.*/
chunk=j*64 /*calculate the size of the chunks. */
do k=1 for 16 /*process the message in chunks. */
!.k=reverse( substr($, chunk + 1 + 4*(k-1), 4) ) /*magic stuff.*/
end /*k*/ /*────step────*/
a = .part1( a, b, c, d, 0, 7, 3614090360) /*■■■■ 1 ■■■■*/
d = .part1( d, a, b, c, 1, 12, 3905402710) /*■■■■ 2 ■■■■*/
c = .part1( c, d, a, b, 2, 17, 606105819) /*■■■■ 3 ■■■■*/
b = .part1( b, c, d, a, 3, 22, 3250441966) /*■■■■ 4 ■■■■*/
a = .part1( a, b, c, d, 4, 7, 4118548399) /*■■■■ 5 ■■■■*/
d = .part1( d, a, b, c, 5, 12, 1200080426) /*■■■■ 6 ■■■■*/
c = .part1( c, d, a, b, 6, 17, 2821735955) /*■■■■ 7 ■■■■*/
b = .part1( b, c, d, a, 7, 22, 4249261313) /*■■■■ 8 ■■■■*/
a = .part1( a, b, c, d, 8, 7, 1770035416) /*■■■■ 9 ■■■■*/
d = .part1( d, a, b, c, 9, 12, 2336552879) /*■■■■ 10 ■■■■*/
c = .part1( c, d, a, b, 10, 17, 4294925233) /*■■■■ 11 ■■■■*/
b = .part1( b, c, d, a, 11, 22, 2304563134) /*■■■■ 12 ■■■■*/
a = .part1( a, b, c, d, 12, 7, 1804603682) /*■■■■ 13 ■■■■*/
d = .part1( d, a, b, c, 13, 12, 4254626195) /*■■■■ 14 ■■■■*/
c = .part1( c, d, a, b, 14, 17, 2792965006) /*■■■■ 15 ■■■■*/
b = .part1( b, c, d, a, 15, 22, 1236535329) /*■■■■ 16 ■■■■*/
a = .part2( a, b, c, d, 1, 5, 4129170786) /*■■■■ 17 ■■■■*/
d = .part2( d, a, b, c, 6, 9, 3225465664) /*■■■■ 18 ■■■■*/
c = .part2( c, d, a, b, 11, 14, 643717713) /*■■■■ 19 ■■■■*/
b = .part2( b, c, d, a, 0, 20, 3921069994) /*■■■■ 20 ■■■■*/
a = .part2( a, b, c, d, 5, 5, 3593408605) /*■■■■ 21 ■■■■*/
d = .part2( d, a, b, c, 10, 9, 38016083) /*■■■■ 22 ■■■■*/
c = .part2( c, d, a, b, 15, 14, 3634488961) /*■■■■ 23 ■■■■*/
b = .part2( b, c, d, a, 4, 20, 3889429448) /*■■■■ 24 ■■■■*/
a = .part2( a, b, c, d, 9, 5, 568446438) /*■■■■ 25 ■■■■*/
d = .part2( d, a, b, c, 14, 9, 3275163606) /*■■■■ 26 ■■■■*/
c = .part2( c, d, a, b, 3, 14, 4107603335) /*■■■■ 27 ■■■■*/
b = .part2( b, c, d, a, 8, 20, 1163531501) /*■■■■ 28 ■■■■*/
a = .part2( a, b, c, d, 13, 5, 2850285829) /*■■■■ 29 ■■■■*/
d = .part2( d, a, b, c, 2, 9, 4243563512) /*■■■■ 30 ■■■■*/
c = .part2( c, d, a, b, 7, 14, 1735328473) /*■■■■ 31 ■■■■*/
b = .part2( b, c, d, a, 12, 20, 2368359562) /*■■■■ 32 ■■■■*/
a = .part3( a, b, c, d, 5, 4, 4294588738) /*■■■■ 33 ■■■■*/
d = .part3( d, a, b, c, 8, 11, 2272392833) /*■■■■ 34 ■■■■*/
c = .part3( c, d, a, b, 11, 16, 1839030562) /*■■■■ 35 ■■■■*/
b = .part3( b, c, d, a, 14, 23, 4259657740) /*■■■■ 36 ■■■■*/
a = .part3( a, b, c, d, 1, 4, 2763975236) /*■■■■ 37 ■■■■*/
d = .part3( d, a, b, c, 4, 11, 1272893353) /*■■■■ 38 ■■■■*/
c = .part3( c, d, a, b, 7, 16, 4139469664) /*■■■■ 39 ■■■■*/
b = .part3( b, c, d, a, 10, 23, 3200236656) /*■■■■ 40 ■■■■*/
a = .part3( a, b, c, d, 13, 4, 681279174) /*■■■■ 41 ■■■■*/
d = .part3( d, a, b, c, 0, 11, 3936430074) /*■■■■ 42 ■■■■*/
c = .part3( c, d, a, b, 3, 16, 3572445317) /*■■■■ 43 ■■■■*/
b = .part3( b, c, d, a, 6, 23, 76029189) /*■■■■ 44 ■■■■*/
a = .part3( a, b, c, d, 9, 4, 3654602809) /*■■■■ 45 ■■■■*/
d = .part3( d, a, b, c, 12, 11, 3873151461) /*■■■■ 46 ■■■■*/
c = .part3( c, d, a, b, 15, 16, 530742520) /*■■■■ 47 ■■■■*/
b = .part3( b, c, d, a, 2, 23, 3299628645) /*■■■■ 48 ■■■■*/
a = .part4( a, b, c, d, 0, 6, 4096336452) /*■■■■ 49 ■■■■*/
d = .part4( d, a, b, c, 7, 10, 1126891415) /*■■■■ 50 ■■■■*/
c = .part4( c, d, a, b, 14, 15, 2878612391) /*■■■■ 51 ■■■■*/
b = .part4( b, c, d, a, 5, 21, 4237533241) /*■■■■ 52 ■■■■*/
a = .part4( a, b, c, d, 12, 6, 1700485571) /*■■■■ 53 ■■■■*/
d = .part4( d, a, b, c, 3, 10, 2399980690) /*■■■■ 54 ■■■■*/
c = .part4( c, d, a, b, 10, 15, 4293915773) /*■■■■ 55 ■■■■*/
b = .part4( b, c, d, a, 1, 21, 2240044497) /*■■■■ 56 ■■■■*/
a = .part4( a, b, c, d, 8, 6, 1873313359) /*■■■■ 57 ■■■■*/
d = .part4( d, a, b, c, 15, 10, 4264355552) /*■■■■ 58 ■■■■*/
c = .part4( c, d, a, b, 6, 15, 2734768916) /*■■■■ 59 ■■■■*/
b = .part4( b, c, d, a, 13, 21, 1309151649) /*■■■■ 60 ■■■■*/
a = .part4( a, b, c, d, 4, 6, 4149444226) /*■■■■ 61 ■■■■*/
d = .part4( d, a, b, c, 11, 10, 3174756917) /*■■■■ 62 ■■■■*/
c = .part4( c, d, a, b, 2, 15, 718787259) /*■■■■ 63 ■■■■*/
b = .part4( b, c, d, a, 9, 21, 3951481745) /*■■■■ 64 ■■■■*/
a = .a(a_, a); b=.a(b_, b); c=.a(c_, c); d=.a(d_, d)
end /*j*/
a = .part1( a, b, c, d, 0, 7, 3614090360) /*■■■■1■■■*/
d = .part1( d, a, b, c, 1, 12, 3905402710) /*■■■■2■■■*/
c = .part1( c, d, a, b, 2, 17, 606105819) /*■■■■3■■■*/
b = .part1( b, c, d, a, 3, 22, 3250441966) /*■■■■4■■■*/
a = .part1( a, b, c, d, 4, 7, 4118548399) /*■■■■5■■■*/
d = .part1( d, a, b, c, 5, 12, 1200080426) /*■■■■6■■■*/
c = .part1( c, d, a, b, 6, 17, 2821735955) /*■■■■7■■■*/
b = .part1( b, c, d, a, 7, 22, 4249261313) /*■■■■8■■■*/
a = .part1( a, b, c, d, 8, 7, 1770035416) /*■■■■9■■■*/
d = .part1( d, a, b, c, 9, 12, 2336552879) /*■■■10■■■*/
c = .part1( c, d, a, b, 10, 17, 4294925233) /*■■■11■■■*/
b = .part1( b, c, d, a, 11, 22, 2304563134) /*■■■12■■■*/
a = .part1( a, b, c, d, 12, 7, 1804603682) /*■■■13■■■*/
d = .part1( d, a, b, c, 13, 12, 4254626195) /*■■■14■■■*/
c = .part1( c, d, a, b, 14, 17, 2792965006) /*■■■15■■■*/
b = .part1( b, c, d, a, 15, 22, 1236535329) /*■■■16■■■*/
a = .part2( a, b, c, d, 1, 5, 4129170786) /*■■■17■■■*/
d = .part2( d, a, b, c, 6, 9, 3225465664) /*■■■18■■■*/
c = .part2( c, d, a, b, 11, 14, 643717713) /*■■■19■■■*/
b = .part2( b, c, d, a, 0, 20, 3921069994) /*■■■20■■■*/
a = .part2( a, b, c, d, 5, 5, 3593408605) /*■■■21■■■*/
d = .part2( d, a, b, c, 10, 9, 38016083) /*■■■22■■■*/
c = .part2( c, d, a, b, 15, 14, 3634488961) /*■■■23■■■*/
b = .part2( b, c, d, a, 4, 20, 3889429448) /*■■■24■■■*/
a = .part2( a, b, c, d, 9, 5, 568446438) /*■■■25■■■*/
d = .part2( d, a, b, c, 14, 9, 3275163606) /*■■■26■■■*/
c = .part2( c, d, a, b, 3, 14, 4107603335) /*■■■27■■■*/
b = .part2( b, c, d, a, 8, 20, 1163531501) /*■■■28■■■*/
a = .part2( a, b, c, d, 13, 5, 2850285829) /*■■■29■■■*/
d = .part2( d, a, b, c, 2, 9, 4243563512) /*■■■30■■■*/
c = .part2( c, d, a, b, 7, 14, 1735328473) /*■■■31■■■*/
b = .part2( b, c, d, a, 12, 20, 2368359562) /*■■■32■■■*/
a = .part3( a, b, c, d, 5, 4, 4294588738) /*■■■33■■■*/
d = .part3( d, a, b, c, 8, 11, 2272392833) /*■■■34■■■*/
c = .part3( c, d, a, b, 11, 16, 1839030562) /*■■■35■■■*/
b = .part3( b, c, d, a, 14, 23, 4259657740) /*■■■36■■■*/
a = .part3( a, b, c, d, 1, 4, 2763975236) /*■■■37■■■*/
d = .part3( d, a, b, c, 4, 11, 1272893353) /*■■■38■■■*/
c = .part3( c, d, a, b, 7, 16, 4139469664) /*■■■39■■■*/
b = .part3( b, c, d, a, 10, 23, 3200236656) /*■■■40■■■*/
a = .part3( a, b, c, d, 13, 4, 681279174) /*■■■41■■■*/
d = .part3( d, a, b, c, 0, 11, 3936430074) /*■■■42■■■*/
c = .part3( c, d, a, b, 3, 16, 3572445317) /*■■■43■■■*/
b = .part3( b, c, d, a, 6, 23, 76029189) /*■■■44■■■*/
a = .part3( a, b, c, d, 9, 4, 3654602809) /*■■■45■■■*/
d = .part3( d, a, b, c, 12, 11, 3873151461) /*■■■46■■■*/
c = .part3( c, d, a, b, 15, 16, 530742520) /*■■■47■■■*/
b = .part3( b, c, d, a, 2, 23, 3299628645) /*■■■48■■■*/
a = .part4( a, b, c, d, 0, 6, 4096336452) /*■■■49■■■*/
d = .part4( d, a, b, c, 7, 10, 1126891415) /*■■■50■■■*/
c = .part4( c, d, a, b, 14, 15, 2878612391) /*■■■51■■■*/
b = .part4( b, c, d, a, 5, 21, 4237533241) /*■■■52■■■*/
a = .part4( a, b, c, d, 12, 6, 1700485571) /*■■■53■■■*/
d = .part4( d, a, b, c, 3, 10, 2399980690) /*■■■54■■■*/
c = .part4( c, d, a, b, 10, 15, 4293915773) /*■■■55■■■*/
b = .part4( b, c, d, a, 1, 21, 2240044497) /*■■■56■■■*/
a = .part4( a, b, c, d, 8, 6, 1873313359) /*■■■57■■■*/
d = .part4( d, a, b, c, 15, 10, 4264355552) /*■■■58■■■*/
c = .part4( c, d, a, b, 6, 15, 2734768916) /*■■■59■■■*/
b = .part4( b, c, d, a, 13, 21, 1309151649) /*■■■60■■■*/
a = .part4( a, b, c, d, 4, 6, 4149444226) /*■■■61■■■*/
d = .part4( d, a, b, c, 11, 10, 3174756917) /*■■■62■■■*/
c = .part4( c, d, a, b, 2, 15, 718787259) /*■■■63■■■*/
b = .part4( b, c, d, a, 9, 21, 3951481745) /*■■■64■■■*/
a = .a(a_,a); b=.a(b_,b); c=.a(c_,c); d=.a(d_,d)
end /*j*/
return c2x(reverse(a))c2x(reverse(b))c2x(reverse(c))c2x(reverse(d))
/*─────────────────────────────────────subroutines─────────────────────────────────────*/
.a: return right(d2c(c2d(arg(1)) + c2d(arg(2))), 4, '0'x)
.h: return bitxor(bitxor(arg(1), arg(2)), arg(3))
.i: return bitxor(arg(2), bitor(arg(1), bitxor(arg(3), 'ffffffff'x)))
.f: return bitor(bitand(arg(1),arg(2)), bitand(bitxor(arg(1), 'ffffffff'x), arg(3)))
.g: return bitor(bitand(arg(1),arg(3)), bitand(arg(2), bitxor(arg(3), 'ffffffff'x)))
.Lr: procedure; parse arg _,#; if #==0 then return _ /*left rotate.*/
?=x2b(c2x(_)); return x2c(b2x(right(? || left(?, #), length(?))))
return c2x( reverse(a) )c2x( reverse(b) )c2x( reverse(c) )c2x( reverse(d) )
/*──────────────────────────────────────────────────────────────────────────────────────*/
.a: return right( d2c( c2d( arg(1) ) + c2d( arg(2) ) ), 4, '0'x)
.h: return bitxor( bitxor( arg(1), arg(2) ), arg(3) )
.i: return bitxor( arg(2), bitor(arg(1), bitxor(arg(3), 'ffffffff'x)))
.f: return bitor( bitand(arg(1),arg(2)), bitand(bitxor(arg(1), 'ffffffff'x), arg(3)))
.g: return bitor( bitand(arg(1),arg(3)), bitand(arg(2), bitxor(arg(3), 'ffffffff'x)))
.Lr: procedure; parse arg _,#; if #==0 then return _ /*left rotate.*/
?=x2b(c2x(_)); return x2c( b2x( right(? || left(?, #), length(?) )))
.part1: procedure expose !.; parse arg w,x,y,z,n,m,_; n=n+1
return .a(.Lr(right(d2c(_+c2d(w)+c2d(.f(x,y,z))+c2d(!.n)),4,'0'x),m),x)
return .a(.Lr(right(d2c(_+c2d(w) +c2d(.f(x,y,z))+c2d(!.n)),4,'0'x),m),x)
.part2: procedure expose !.; parse arg w,x,y,z,n,m,_; n=n+1
return .a(.Lr(right(d2c(_+c2d(w)+c2d(.g(x,y,z))+c2d(!.n)),4,'0'x),m),x)
return .a(.Lr(right(d2c(_+c2d(w) +c2d(.g(x,y,z))+c2d(!.n)),4,'0'x),m),x)
.part3: procedure expose !.; parse arg w,x,y,z,n,m,_; n=n+1
return .a(.Lr(right(d2c(_+c2d(w)+c2d(.h(x,y,z))+c2d(!.n)),4,'0'x),m),x)
return .a(.Lr(right(d2c(_+c2d(w) +c2d(.h(x,y,z))+c2d(!.n)),4,'0'x),m),x)
.part4: procedure expose !.; parse arg w,x,y,z,n,m,_; n=n+1
return .a(.Lr(right(d2c(c2d(w)+c2d(.i(x,y,z))+c2d(!.n)+_),4,'0'x),m),x)
return .a(.Lr(right(d2c(c2d(w) +c2d(.i(x,y,z))+c2d(!.n)+_),4,'0'x),m),x)

89
Task/MD5/RPG/md5.rpg Normal file
View file

@ -0,0 +1,89 @@
**FREE
Ctl-opt MAIN(Main);
Ctl-opt DFTACTGRP(*NO) ACTGRP(*NEW);
dcl-pr QDCXLATE EXTPGM('QDCXLATE');
dataLen packed(5 : 0) CONST;
data char(32767) options(*VARSIZE);
conversionTable char(10) CONST;
end-pr;
dcl-pr Qc3CalculateHash EXTPROC('Qc3CalculateHash');
inputData pointer value;
inputDataLen int(10) const;
inputDataFormat char(8) const;
algorithmDscr char(16) const;
algorithmFormat char(8) const;
cryptoServiceProvider char(1) const;
cryptoDeviceName char(1) const options(*OMIT);
hash char(64) options(*VARSIZE : *OMIT);
errorCode char(32767) options(*VARSIZE);
end-pr;
dcl-c HEX_CHARS CONST('0123456789ABCDEF');
dcl-proc Main;
dcl-s inputData char(45);
dcl-s inputDataLen int(10) INZ(0);
dcl-s outputHash char(16);
dcl-s outputHashHex char(32);
dcl-ds algorithmDscr QUALIFIED;
hashAlgorithm int(10) INZ(0);
end-ds;
dcl-ds ERRC0100_NULL QUALIFIED;
bytesProvided int(10) INZ(0); // Leave at zero
bytesAvailable int(10);
end-ds;
dow inputDataLen = 0;
DSPLY 'Input: ' '' inputData;
inputData = %trim(inputData);
inputDataLen = %len(%trim(inputData));
DSPLY ('Input=' + inputData);
DSPLY ('InputLen=' + %char(inputDataLen));
if inputDataLen = 0;
DSPLY 'Input must not be blank';
endif;
enddo;
// Convert from EBCDIC to ASCII
QDCXLATE(inputDataLen : inputData : 'QTCPASC');
algorithmDscr.hashAlgorithm = 1; // MD5
// Calculate hash
Qc3CalculateHash(%addr(inputData) : inputDataLen : 'DATA0100' : algorithmDscr
: 'ALGD0500' : '0' : *OMIT : outputHash : ERRC0100_NULL);
// Convert to hex
CVTHC(outputHashHex : outputHash : 32);
DSPLY ('MD5: ' + outputHashHex);
return;
end-proc;
// This procedure is actually a MI, but I couldn't get it to bind so I wrote my own version
dcl-proc CVTHC;
dcl-pi *N;
target char(65534) options(*VARSIZE);
srcBits char(32767) options(*VARSIZE) CONST;
targetLen int(10) value;
end-pi;
dcl-s i int(10);
dcl-s lowNibble ind INZ(*OFF);
dcl-s inputOffset int(10) INZ(1);
dcl-ds dataStruct QUALIFIED;
numField int(5) INZ(0);
// IBM i is big-endian
charField char(1) OVERLAY(numField : 2);
end-ds;
for i = 1 to targetLen;
if lowNibble;
dataStruct.charField = %BitAnd(%subst(srcBits : inputOffset : 1) : X'0F');
inputOffset += 1;
else;
dataStruct.charField = %BitAnd(%subst(srcBits : inputOffset : 1) : X'F0');
dataStruct.numField /= 16;
endif;
%subst(target : i : 1) = %subst(HEX_CHARS : dataStruct.numField + 1 : 1);
lowNibble = NOT lowNibble;
endfor;
return;
end-proc;

View file

@ -0,0 +1,2 @@
require("chksum");
print(md5sum("The quick brown fox jumped over the lazy dog's back"));