Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
|
|
@ -23,4 +23,4 @@ In addition, intermediate outputs to aid in developing an implementation can be
|
|||
|
||||
The MD5 Message-Digest Algorithm was developed by [[wp:RSA_SecurityRSA|RSA Data Security, Inc.]] in 1991.
|
||||
|
||||
{{alertbox|#ffff70|'''<big>Warning</big>'''<br/>Rosetta Code is '''not''' a place you should rely on for examples of code in critical roles, including security.<br/>Also, note that MD5 has been ''broken'' and should not be used applications requiring security. For these consider [[wp:SHA2|SHA2]] or the upcoming [[wp:SHA3|SHA3]].}}
|
||||
{{alertbox|#ffff70|'''<big>Warning</big>'''<br/>Rosetta Code is '''not''' a place you should rely on for examples of code in critical roles, including security.<br/>Also, note that MD5 has been ''broken'' and should not be used in applications requiring security. For these consider [[wp:SHA2|SHA2]] or the upcoming [[wp:SHA3|SHA3]].}}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
|
||||
byte[] bs = System.Text.Encoding.UTF8.GetBytes(password);
|
||||
bs = x.ComputeHash(bs);
|
||||
bs = x.ComputeHash(bs); //this function is not in the above classdefinition
|
||||
System.Text.StringBuilder s = new System.Text.StringBuilder();
|
||||
foreach (byte b in bs)
|
||||
{
|
||||
|
|
|
|||
|
|
@ -0,0 +1,65 @@
|
|||
# Array sum helper function.
|
||||
sum = (array) ->
|
||||
array.reduce (x, y) -> x + y
|
||||
|
||||
md5 = do ->
|
||||
# Per-round shift amounts.
|
||||
s = [738695, 669989, 770404, 703814]
|
||||
s = (s[i >> 4] >> i % 4 * 5 & 31 for i in [0..63])
|
||||
|
||||
# Constants cache generated by sine.
|
||||
K = (Math.floor 2**32 * Math.abs Math.sin i for i in [1..64])
|
||||
|
||||
# Bitwise left rotate helper function.
|
||||
lrot = (x, y) ->
|
||||
x << y | x >>> 32 - y;
|
||||
|
||||
(input) ->
|
||||
# Initialize values.
|
||||
d0 = 0x10325476;
|
||||
a0 = 0x67452301;
|
||||
b0 = ~d0
|
||||
c0 = ~a0;
|
||||
|
||||
# Convert the message to 32-bit words, little-endian.
|
||||
M =
|
||||
for i in [0...input.length] by 4
|
||||
sum (input.charCodeAt(i + j) << j*8 for j in [0..3])
|
||||
|
||||
# Pre-processing: append a 1 bit, then message length % 2^64.
|
||||
len = input.length * 8
|
||||
M[len >> 5] |= 128 << len % 32
|
||||
M[(len + 64 >>> 9 << 4) + 14] = len
|
||||
|
||||
# Process the message in chunks of 16 32-bit words.
|
||||
for x in [0...M.length] by 16
|
||||
[A, B, C, D] = [a0, b0, c0, d0]
|
||||
|
||||
# Main loop.
|
||||
for i in [0..63]
|
||||
if i < 16
|
||||
F = B & C | ~B & D
|
||||
g = i
|
||||
else if i < 32
|
||||
F = B & D | C & ~D
|
||||
g = i * 5 + 1
|
||||
else if i < 48
|
||||
F = B ^ C ^ D
|
||||
g = i * 3 + 5
|
||||
else
|
||||
F = C ^ (B | ~D)
|
||||
g = i * 7
|
||||
|
||||
[A, B, C, D] =
|
||||
[D, B + lrot(A + F + K[i] + (M[x + g % 16] ? 0), s[i]), B, C]
|
||||
|
||||
a0 += A
|
||||
b0 += B
|
||||
c0 += C
|
||||
d0 += D
|
||||
|
||||
# Convert the four words back to a string.
|
||||
return (
|
||||
for x in [a0, b0, c0, d0]
|
||||
(String.fromCharCode x >>> 8 * y & 255 for y in [0..3]).join ''
|
||||
).join ''
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
str2hex = do ->
|
||||
hex = ['0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f']
|
||||
hex = (hex[x >> 4] + hex[x & 15] for x in [0..255])
|
||||
(str) ->
|
||||
(hex[c.charCodeAt()] for c in str).join ''
|
||||
|
||||
console.log str2hex md5 message for message in [
|
||||
""
|
||||
"a"
|
||||
"abc"
|
||||
"message digest"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"
|
||||
"12345678901234567890123456789012345678901234567890123456789012345678901234567890"
|
||||
]
|
||||
|
|
@ -3,6 +3,8 @@ package main
|
|||
import (
|
||||
"fmt"
|
||||
"math"
|
||||
"bytes"
|
||||
"encoding/binary"
|
||||
)
|
||||
|
||||
type testCase struct {
|
||||
|
|
@ -38,22 +40,17 @@ func init() {
|
|||
}
|
||||
|
||||
func md5(s string) (r [16]byte) {
|
||||
numBlocks := (len(s) + 72) >> 6
|
||||
padded := make([]byte, numBlocks<<6)
|
||||
copy(padded, s)
|
||||
padded[len(s)] = 0x80
|
||||
for i, messageLenBits := len(padded)-8, len(s)<<3; i < len(padded); i++ {
|
||||
padded[i] = byte(messageLenBits)
|
||||
messageLenBits >>= 8
|
||||
padded := bytes.NewBuffer([]byte(s))
|
||||
padded.WriteByte(0x80)
|
||||
for padded.Len() % 64 != 56 {
|
||||
padded.WriteByte(0)
|
||||
}
|
||||
messageLenBits := uint64(len(s)) * 8
|
||||
binary.Write(padded, binary.LittleEndian, messageLenBits)
|
||||
|
||||
var a, b, c, d uint32 = 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476
|
||||
var buffer [16]uint32
|
||||
for i := 0; i < numBlocks; i++ {
|
||||
index := i << 6
|
||||
for j := 0; j < 64; j, index = j+1, index+1 {
|
||||
buffer[j>>2] = (uint32(padded[index]) << 24) | (buffer[j>>2] >> 8)
|
||||
}
|
||||
for binary.Read(padded, binary.LittleEndian, buffer[:]) == nil { // read every 64 bytes
|
||||
a1, b1, c1, d1 := a, b, c, d
|
||||
for j := 0; j < 64; j++ {
|
||||
var f uint32
|
||||
|
|
@ -79,11 +76,6 @@ func md5(s string) (r [16]byte) {
|
|||
a, b, c, d = a+a1, b+b1, c+c1, d+d1
|
||||
}
|
||||
|
||||
for i, n := range []uint32{a, b, c, d} {
|
||||
for j := 0; j < 4; j++ {
|
||||
r[i*4+j] = byte(n)
|
||||
n >>= 8
|
||||
}
|
||||
}
|
||||
binary.Write(bytes.NewBuffer(r[:0]), binary.LittleEndian, []uint32{a, b, c, d})
|
||||
return
|
||||
}
|
||||
|
|
|
|||
114
Task/MD5-Implementation/Java/md5-implementation-2.java
Normal file
114
Task/MD5-Implementation/Java/md5-implementation-2.java
Normal file
|
|
@ -0,0 +1,114 @@
|
|||
import java.nio.ByteBuffer;
|
||||
import java.nio.ByteOrder;
|
||||
|
||||
class MD5
|
||||
{
|
||||
|
||||
private static final int INIT_A = 0x67452301;
|
||||
private static final int INIT_B = (int)0xEFCDAB89L;
|
||||
private static final int INIT_C = (int)0x98BADCFEL;
|
||||
private static final int INIT_D = 0x10325476;
|
||||
|
||||
private static final int[] SHIFT_AMTS = {
|
||||
7, 12, 17, 22,
|
||||
5, 9, 14, 20,
|
||||
4, 11, 16, 23,
|
||||
6, 10, 15, 21
|
||||
};
|
||||
|
||||
private static final int[] TABLE_T = new int[64];
|
||||
static
|
||||
{
|
||||
for (int i = 0; i < 64; i++)
|
||||
TABLE_T[i] = (int)(long)((1L << 32) * Math.abs(Math.sin(i + 1)));
|
||||
}
|
||||
|
||||
public static byte[] computeMD5(byte[] message)
|
||||
{
|
||||
ByteBuffer padded = ByteBuffer.allocate((((message.length + 8) / 64) + 1) * 64).order(ByteOrder.LITTLE_ENDIAN);
|
||||
padded.put(message);
|
||||
padded.put((byte)0x80);
|
||||
long messageLenBits = (long)message.length * 8;
|
||||
padded.putLong(padded.capacity() - 8, messageLenBits);
|
||||
|
||||
padded.rewind();
|
||||
|
||||
int a = INIT_A;
|
||||
int b = INIT_B;
|
||||
int c = INIT_C;
|
||||
int d = INIT_D;
|
||||
while (padded.hasRemaining()) {
|
||||
// obtain a slice of the buffer from the current position,
|
||||
// and view it as an array of 32-bit ints
|
||||
IntBuffer chunk = padded.slice().order(ByteOrder.LITTLE_ENDIAN).asIntBuffer();
|
||||
int originalA = a;
|
||||
int originalB = b;
|
||||
int originalC = c;
|
||||
int originalD = d;
|
||||
for (int j = 0; j < 64; j++)
|
||||
{
|
||||
int div16 = j >>> 4;
|
||||
int f = 0;
|
||||
int bufferIndex = j;
|
||||
switch (div16)
|
||||
{
|
||||
case 0:
|
||||
f = (b & c) | (~b & d);
|
||||
break;
|
||||
|
||||
case 1:
|
||||
f = (b & d) | (c & ~d);
|
||||
bufferIndex = (bufferIndex * 5 + 1) & 0x0F;
|
||||
break;
|
||||
|
||||
case 2:
|
||||
f = b ^ c ^ d;
|
||||
bufferIndex = (bufferIndex * 3 + 5) & 0x0F;
|
||||
break;
|
||||
|
||||
case 3:
|
||||
f = c ^ (b | ~d);
|
||||
bufferIndex = (bufferIndex * 7) & 0x0F;
|
||||
break;
|
||||
}
|
||||
int temp = b + Integer.rotateLeft(a + f + chunk.get(bufferIndex) + TABLE_T[j], SHIFT_AMTS[(div16 << 2) | (j & 3)]);
|
||||
a = d;
|
||||
d = c;
|
||||
c = b;
|
||||
b = temp;
|
||||
}
|
||||
|
||||
a += originalA;
|
||||
b += originalB;
|
||||
c += originalC;
|
||||
d += originalD;
|
||||
padded.position(padded.position() + 64);
|
||||
}
|
||||
|
||||
ByteBuffer md5 = ByteBuffer.allocate(16).order(ByteOrder.LITTLE_ENDIAN);
|
||||
for (int n : new int[]{a, b, c, d})
|
||||
{
|
||||
md5.putInt(n);
|
||||
}
|
||||
return md5.array();
|
||||
}
|
||||
|
||||
public static String toHexString(byte[] b)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < b.length; i++)
|
||||
{
|
||||
sb.append(String.format("%02X", b[i] & 0xFF));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static void main(String[] args)
|
||||
{
|
||||
String[] testStrings = { "", "a", "abc", "message digest", "abcdefghijklmnopqrstuvwxyz", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789", "12345678901234567890123456789012345678901234567890123456789012345678901234567890" };
|
||||
for (String s : testStrings)
|
||||
System.out.println("0x" + toHexString(computeMD5(s.getBytes())) + " <== \"" + s + "\"");
|
||||
return;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -9,25 +9,29 @@ constant FGHI = -> \X, \Y, \Z { (X +& Y) +| (¬X +& Z) },
|
|||
-> \X, \Y, \Z { X +^ Y +^ Z },
|
||||
-> \X, \Y, \Z { Y +^ (X +| ¬Z) };
|
||||
|
||||
constant S = (7, 12, 17, 22) xx 4,
|
||||
(5, 9, 14, 20) xx 4,
|
||||
(4, 11, 16, 23) xx 4,
|
||||
(6, 10, 15, 21) xx 4;
|
||||
constant S = flat (7, 12, 17, 22) xx 4,
|
||||
(5, 9, 14, 20) xx 4,
|
||||
(4, 11, 16, 23) xx 4,
|
||||
(6, 10, 15, 21) xx 4;
|
||||
|
||||
constant T = (floor(abs(sin($_ + 1)) * 2**32) for ^64);
|
||||
|
||||
constant k = ( $_ for ^16),
|
||||
((5*$_ + 1) % 16 for ^16),
|
||||
((3*$_ + 5) % 16 for ^16),
|
||||
((7*$_ ) % 16 for ^16);
|
||||
constant k = flat ( $_ for ^16),
|
||||
((5*$_ + 1) % 16 for ^16),
|
||||
((3*$_ + 5) % 16 for ^16),
|
||||
((7*$_ ) % 16 for ^16);
|
||||
|
||||
sub little-endian($w, $n, *@v) { (@v X+> ($w X* ^$n)) X% (2 ** $w) }
|
||||
sub little-endian($w, $n, *@v) {
|
||||
my \step1 = ($w X* ^$n).eager; # temporary bug workaround
|
||||
my \step2 = (@v X+> step1);
|
||||
step2 X% (2 ** $w);
|
||||
}
|
||||
|
||||
sub md5-pad(Blob $msg)
|
||||
{
|
||||
my \bits = 8 * $msg.elems;
|
||||
my @padded = $msg.list, 0x80, 0x00 xx (-(bits div 8 + 1 + 8) % 64);
|
||||
@padded.map({ :256[$^d,$^c,$^b,$^a] }), little-endian(32, 2, bits);
|
||||
my @padded = flat $msg.list, 0x80, 0x00 xx (-(bits div 8 + 1 + 8) % 64);
|
||||
flat @padded.map({ :256[$^d,$^c,$^b,$^a] }), little-endian(32, 2, bits);
|
||||
}
|
||||
|
||||
sub md5-block(@H is rw, @X)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue