Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -1,4 +1,6 @@
Find the MD4 message digest of a string of [[octet]]s. Use the ASCII encoded string “<tt>Rosetta Code</tt>” (without quotes). You may either call an MD4 library, or implement MD4 in your language.
Find the MD4 message digest of a string of [[octet]]s.
Use the ASCII encoded string “<tt>Rosetta Code</tt>” (without quotes).
You may either call an MD4 library, or implement MD4 in your language.
'''MD4''' is an obsolete hash function that computes a 128-bit message digest that sometimes appears in obsolete protocols.

2
Task/MD4/Clojure/md4.clj Normal file
View file

@ -0,0 +1,2 @@
(use 'pandect.core)
(md4 "Rosetta Code")

47
Task/MD4/D/md4.d Normal file
View file

@ -0,0 +1,47 @@
import std.stdio, std.string, std.range;
ubyte[16] md4(const(ubyte)[] inData) pure nothrow {
enum f = (uint x, uint y, uint z) => (x & y) | (~x & z);
enum g = (uint x, uint y, uint z) => (x & y) | (x & z) | (y & z);
enum h = (uint x, uint y, uint z) => x ^ y ^ z;
enum r = (uint v, uint s) => (v << s) | (v >> (32 - s));
immutable bitLen = ulong(inData.length) << 3;
inData ~= 0x80;
while (inData.length % 64 != 56)
inData ~= 0;
const data = cast(uint[])inData ~ [uint(bitLen & uint.max), uint(bitLen >> 32)];
uint a = 0x67452301, b = 0xefcdab89, c = 0x98badcfe, d = 0x10325476;
foreach (const x; data.chunks(16)) {
immutable a2 = a, b2 = b, c2 = c, d2 = d;
foreach (immutable i; [0, 4, 8, 12]) {
a = r(a + f(b, c, d) + x[i+0], 3);
d = r(d + f(a, b, c) + x[i+1], 7);
c = r(c + f(d, a, b) + x[i+2], 11);
b = r(b + f(c, d, a) + x[i+3], 19);
}
foreach (immutable i; [0, 1, 2, 3]) {
a = r(a + g(b, c, d) + x[i+0] + 0x5a827999, 3);
d = r(d + g(a, b, c) + x[i+4] + 0x5a827999, 5);
c = r(c + g(d, a, b) + x[i+8] + 0x5a827999, 9);
b = r(b + g(c, d, a) + x[i+12] + 0x5a827999, 13);
}
foreach (immutable i; [0, 2, 1, 3]) {
a = r(a + h(b, c, d) + x[i+0] + 0x6ed9eba1, 3);
d = r(d + h(a, b, c) + x[i+8] + 0x6ed9eba1, 9);
c = r(c + h(d, a, b) + x[i+4] + 0x6ed9eba1, 11);
b = r(b + h(c, d, a) + x[i+12] + 0x6ed9eba1, 15);
}
a += a2, b += b2, c += c2, d += d2;
}
//return cast(ubyte[16])[a, b, c, d];
immutable uint[4] result = [a, b, c, d];
return cast(ubyte[16])result;
}
void main() {
writefln("%(%02x%)", "Rosetta Code".representation.md4);
}

View file

@ -1,7 +1,7 @@
package main
import (
"code.google.com/p/go.crypto/md4"
"golang.org/x/crypto/md4"
"fmt"
)

176
Task/MD4/PicoLisp/md4.l Normal file
View file

@ -0,0 +1,176 @@
(de *Md4-W .
(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
1 5 9 13 2 6 10 14 3 7 11 15 4 8 12 16
1 9 5 13 3 11 7 15 2 10 6 14 4 12 8 16 .))
(de *Md4-R1 . (3 7 11 19 .))
(de *Md4-R2 . (3 5 9 13 .))
(de *Md4-R3 . (3 9 11 15 .))
(de mod32 (N)
(& N `(hex "FFFFFFFF")) )
(de not32 (N)
(x| N `(hex "FFFFFFFF")) )
(de add32 @
(mod32 (pass +)) )
(de leftRotate (X C)
(| (mod32 (>> (- C) X)) (>> (- 32 C) X)) )
(de md4 (Str)
(let Len (length Str)
(setq Str
(conc
(need
(- 8 (* 64 (/ (+ Len 1 8 63) 64))) # Pad to 64-8 bytes
(conc
(mapcar char (chop Str)) # Works only with ASCII characters
(cons `(hex "80")) ) # '1' bit
0 ) # Pad with '0'
(make
(setq Len (* 8 Len))
(do 8
(link (& Len 255))
(setq Len (>> 8 Len )) ) ) ) ) )
(let
(H0 `(hex "67452301")
H1 `(hex "EFCDAB89")
H2 `(hex "98BADCFE")
H3 `(hex "10325476")
R2 `(hex "5A827999")
R3 `(hex "6ED9EBA1") )
(while Str
(let
(A H0 B H1 C H2 D H3
W (make
(do 16
(link
(apply |
(mapcar >> (0 -8 -16 -24) (cut 4 'Str)) ) ) ) ) )
(for I 12
(cond
((>= 4 I)
(setq
A (leftRotate
(add32
A
(| (& B C) (& (not32 B) D))
(get W (pop '*Md4-W)) )
(pop '*Md4-R1) )
D (leftRotate
(add32
D
(| (& A B) (& (not32 A) C))
(get W (pop '*Md4-W)) )
(pop '*Md4-R1) )
C (leftRotate
(add32
C
(| (& D A) (& (not32 D) B))
(get W (pop '*Md4-W)) )
(pop '*Md4-R1) )
B (leftRotate
(add32
B
(| (& C D) (& (not32 C) A))
(get W (pop '*Md4-W)) )
(pop '*Md4-R1) ) ) )
((>= 8 I)
(setq
A (leftRotate
(add32
A
(|
(& B (| C D))
(& C D) )
(get W (pop '*Md4-W))
R2 )
(pop '*Md4-R2) )
D (leftRotate
(add32
D
(|
(& A (| B C))
(& B C) )
(get W (pop '*Md4-W))
R2 )
(pop '*Md4-R2) )
C (leftRotate
(add32
C
(|
(& D (| A B))
(& A B) )
(get W (pop '*Md4-W))
R2 )
(pop '*Md4-R2) )
B (leftRotate
(add32
B
(|
(& C (| D A))
(& D A) )
(get W (pop '*Md4-W))
R2 )
(pop '*Md4-R2) ) ) )
(T
(setq
A (leftRotate
(add32
A
(x| B C D)
(get W (pop '*Md4-W))
R3 )
(pop '*Md4-R3) )
D (leftRotate
(add32
D
(x| A B C)
(get W (pop '*Md4-W))
R3 )
(pop '*Md4-R3) )
C (leftRotate
(add32
C
(x| D A B)
(get W (pop '*Md4-W))
R3 )
(pop '*Md4-R3) )
B (leftRotate
(add32
B
(x| C D A)
(get W (pop '*Md4-W))
R3 )
(pop '*Md4-R3) ) ) ) ) )
(setq
H0 (add32 H0 A)
H1 (add32 H1 B)
H2 (add32 H2 C)
H3 (add32 H3 D) ) ) )
(make
(for N (list H0 H1 H2 H3)
(do 4
(link (& N 255))
(setq N (>> 8 N)) ) ) ) ) )
(let Str "Rosetta Code"
(println
(pack
(mapcar
'((B) (pad 2 (hex B)))
(md4 Str) ) ) )
(println
(pack
(mapcar
'((B) (pad 2 (hex B)))
(native
"libcrypto.so"
"MD4"
'(B . 16)
Str
(length Str)
'(NIL (16)) ) ) ) ) )
(bye)

3
Task/MD4/Racket/md4.rkt Normal file
View file

@ -0,0 +1,3 @@
#lang racket
(require (planet soegaard/digest:1:2/digest))
(md4 #"Rosetta Code")

View file

@ -13,41 +13,22 @@ def md4(string)
# initial hash
a, b, c, d = 0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476
bit_len = string.size << 3
string += "\x80"
while (string.size % 64) != 56
string += "\0"
end
string = string.force_encoding('ascii-8bit') + [bit_len & mask, bit_len >> 32].pack("V2")
if string.size % 64 != 0
fail "failed to pad to correct length"
end
io = StringIO.new(string)
block = ""
term = false # appended "\x80" in second-last block?
last = false # last block?
until last
# Read next block of 16 words (64 bytes, 512 bits).
io.read(64, block) or (
# Work around a bug in Rubinius 1.2.4. At eof,
# MRI and JRuby already replace block with "".
block.replace("")
)
# Unpack block into 32-bit words "V".
case len = block.length
when 64
# Unpack 16 words.
x = block.unpack("V16")
when 56..63
# Second-last block: append padding, unpack 16 words.
block.concat("\x80"); term = true
block.concat("\0" * (63 - len))
x = block.unpack("V16")
when 0..55
# Last block: append padding, unpack 14 words.
block.concat(term ? "\0" : "\x80")
block.concat("\0" * (55 - len))
x = block.unpack("V14")
# Append bit length, 2 words.
bit_len = string.length << 3
x.push(bit_len & mask, bit_len >> 32)
last = true
else
fail "impossible"
end
while io.read(64, block)
x = block.unpack("V16")
# Process this block.
aa, bb, cc, dd = a, b, c, d

12
Task/MD4/Scala/md4.scala Normal file
View file

@ -0,0 +1,12 @@
import org.bouncycastle.crypto.digests.MD4Digest
object RosettaRIPEMD160 extends App {
val (raw, messageDigest) = ("Rosetta Code".getBytes("US-ASCII"), new MD4Digest())
messageDigest.update(raw, 0, raw.length)
val out = Array.fill[Byte](messageDigest.getDigestSize())(0)
messageDigest.doFinal(out, 0)
assert(out.map("%02x".format(_)).mkString == "a52bcfc6a0d0d300cdc5ddbfbefe478b")
import scala.compat.Platform.currentTime
println(s"Successfully completed without errors. [total ${currentTime - executionStart} ms]")
}