|
@@ -32,7 +32,7 @@ hash_string :: proc(data: string) -> [DIGEST_SIZE]byte {
|
|
// computed hash
|
|
// computed hash
|
|
hash_bytes :: proc(data: []byte) -> [DIGEST_SIZE]byte {
|
|
hash_bytes :: proc(data: []byte) -> [DIGEST_SIZE]byte {
|
|
hash: [DIGEST_SIZE]byte
|
|
hash: [DIGEST_SIZE]byte
|
|
- ctx: Md5_Context
|
|
|
|
|
|
+ ctx: Context
|
|
init(&ctx)
|
|
init(&ctx)
|
|
update(&ctx, data)
|
|
update(&ctx, data)
|
|
final(&ctx, hash[:])
|
|
final(&ctx, hash[:])
|
|
@@ -50,7 +50,7 @@ hash_string_to_buffer :: proc(data: string, hash: []byte) {
|
|
// computed hash into the second parameter.
|
|
// computed hash into the second parameter.
|
|
// It requires that the destination buffer is at least as big as the digest size
|
|
// It requires that the destination buffer is at least as big as the digest size
|
|
hash_bytes_to_buffer :: proc(data, hash: []byte) {
|
|
hash_bytes_to_buffer :: proc(data, hash: []byte) {
|
|
- ctx: Md5_Context
|
|
|
|
|
|
+ ctx: Context
|
|
init(&ctx)
|
|
init(&ctx)
|
|
update(&ctx, data)
|
|
update(&ctx, data)
|
|
final(&ctx, hash)
|
|
final(&ctx, hash)
|
|
@@ -60,10 +60,12 @@ hash_bytes_to_buffer :: proc(data, hash: []byte) {
|
|
// hash from its contents
|
|
// hash from its contents
|
|
hash_stream :: proc(s: io.Stream) -> ([DIGEST_SIZE]byte, bool) {
|
|
hash_stream :: proc(s: io.Stream) -> ([DIGEST_SIZE]byte, bool) {
|
|
hash: [DIGEST_SIZE]byte
|
|
hash: [DIGEST_SIZE]byte
|
|
- ctx: Md5_Context
|
|
|
|
|
|
+ ctx: Context
|
|
init(&ctx)
|
|
init(&ctx)
|
|
|
|
+
|
|
buf := make([]byte, 512)
|
|
buf := make([]byte, 512)
|
|
defer delete(buf)
|
|
defer delete(buf)
|
|
|
|
+
|
|
read := 1
|
|
read := 1
|
|
for read > 0 {
|
|
for read > 0 {
|
|
read, _ = io.read(s, buf)
|
|
read, _ = io.read(s, buf)
|
|
@@ -101,7 +103,7 @@ hash :: proc {
|
|
Low level API
|
|
Low level API
|
|
*/
|
|
*/
|
|
|
|
|
|
-init :: proc(ctx: ^Md5_Context) {
|
|
|
|
|
|
+init :: proc(ctx: ^Context) {
|
|
ctx.state[0] = 0x67452301
|
|
ctx.state[0] = 0x67452301
|
|
ctx.state[1] = 0xefcdab89
|
|
ctx.state[1] = 0xefcdab89
|
|
ctx.state[2] = 0x98badcfe
|
|
ctx.state[2] = 0x98badcfe
|
|
@@ -113,7 +115,7 @@ init :: proc(ctx: ^Md5_Context) {
|
|
ctx.is_initialized = true
|
|
ctx.is_initialized = true
|
|
}
|
|
}
|
|
|
|
|
|
-update :: proc(ctx: ^Md5_Context, data: []byte) {
|
|
|
|
|
|
+update :: proc(ctx: ^Context, data: []byte) {
|
|
assert(ctx.is_initialized)
|
|
assert(ctx.is_initialized)
|
|
|
|
|
|
for i := 0; i < len(data); i += 1 {
|
|
for i := 0; i < len(data); i += 1 {
|
|
@@ -127,7 +129,7 @@ update :: proc(ctx: ^Md5_Context, data: []byte) {
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-final :: proc(ctx: ^Md5_Context, hash: []byte) {
|
|
|
|
|
|
+final :: proc(ctx: ^Context, hash: []byte) {
|
|
assert(ctx.is_initialized)
|
|
assert(ctx.is_initialized)
|
|
|
|
|
|
if len(hash) < DIGEST_SIZE {
|
|
if len(hash) < DIGEST_SIZE {
|
|
@@ -171,7 +173,7 @@ final :: proc(ctx: ^Md5_Context, hash: []byte) {
|
|
|
|
|
|
BLOCK_SIZE :: 64
|
|
BLOCK_SIZE :: 64
|
|
|
|
|
|
-Md5_Context :: struct {
|
|
|
|
|
|
+Context :: struct {
|
|
data: [BLOCK_SIZE]byte,
|
|
data: [BLOCK_SIZE]byte,
|
|
state: [4]u32,
|
|
state: [4]u32,
|
|
bitlen: u64,
|
|
bitlen: u64,
|
|
@@ -206,7 +208,7 @@ II :: #force_inline proc "contextless" (a, b, c, d, m: u32, s: int, t: u32) -> u
|
|
}
|
|
}
|
|
|
|
|
|
@(private)
|
|
@(private)
|
|
-transform :: proc "contextless" (ctx: ^Md5_Context, data: []byte) {
|
|
|
|
|
|
+transform :: proc "contextless" (ctx: ^Context, data: []byte) {
|
|
m: [DIGEST_SIZE]u32
|
|
m: [DIGEST_SIZE]u32
|
|
|
|
|
|
for i := 0; i < DIGEST_SIZE; i += 1 {
|
|
for i := 0; i < DIGEST_SIZE; i += 1 {
|