Initial project import

This commit is contained in:
Agila Forge Importer committed 2026-07-13 15:20:32 +00:00
commit 23f2d29e32
11957 files changed
+1754577

No files matched your search

+170
View File
@@ -0,0 +1,170 @@
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
// Package md5 implements the MD5 hash algorithm as defined in RFC 1321.
// MD5 is cryptographically broken and should not be used for secure
// applications.
// Based off: https://github.com/golang/go/blob/master/src/crypto/md5
// Last commit: https://github.com/golang/go/commit/ed7f323c8f4f6bc61a75146bf34f5b8f73063a17
module md5
import encoding.binary
// The size of an MD5 checksum in bytes.
pub const size = 16
// The blocksize of MD5 in bytes.
pub const block_size = 64
const init0 = u32(0x67452301)
const init1 = u32(0xEFCDAB89)
const init2 = u32(0x98BADCFE)
const init3 = u32(0x10325476)
// Digest represents the partial evaluation of a checksum.
struct Digest {
mut:
s []u32
x []u8
nx int
len u64
}
// free the resources taken by the Digest `d`
@[unsafe]
pub fn (mut d Digest) free() {
$if prealloc {
return
}
unsafe { d.x.free() }
}
fn (mut d Digest) init() {
d.s = []u32{len: (4)}
d.x = []u8{len: block_size}
d.reset()
}
// reset the state of the Digest `d`
pub fn (mut d Digest) reset() {
d.s[0] = u32(init0)
d.s[1] = u32(init1)
d.s[2] = u32(init2)
d.s[3] = u32(init3)
d.nx = 0
d.len = 0
}
fn (d &Digest) clone() &Digest {
return &Digest{
...d
s: d.s.clone()
x: d.x.clone()
}
}
// new returns a new Digest (implementing hash.Hash) computing the MD5 checksum.
pub fn new() &Digest {
mut d := &Digest{}
d.init()
return d
}
// write writes the contents of `p_` to the internal hash representation.
pub fn (mut d Digest) write(p_ []u8) !int {
unsafe {
mut p := p_
nn := p.len
d.len += u64(nn)
if d.nx > 0 {
n := copy(mut d.x[d.nx..], p)
d.nx += n
if d.nx == block_size {
block(mut d, d.x)
d.nx = 0
}
if n >= p.len {
p = []
} else {
p = p[n..]
}
}
if p.len >= block_size {
n := p.len & ~(block_size - 1)
block(mut d, p[..n])
if n >= p.len {
p = []
} else {
p = p[n..]
}
}
if p.len > 0 {
d.nx = copy(mut d.x, p)
}
return nn
}
}
// sum returns the md5 sum of the bytes in `b_in`.
pub fn (d &Digest) sum(b_in []u8) []u8 {
// Make a copy of d so that caller can keep writing and summing.
mut d0 := d.clone()
hash := d0.checksum()
mut b_out := b_in.clone()
b_out << hash
return b_out
}
// checksum returns the byte checksum of the `Digest`,
fn (mut d Digest) checksum() []u8 {
// Append 0x80 to the end of the message and then append zeros
// until the length is a multiple of 56 bytes. Finally append
// 8 bytes representing the message length in bits.
//
// 1 byte end marker :: 0-63 padding bytes :: 8 byte length
// tmp := [1 + 63 + 8]u8{0x80}
mut tmp := []u8{len: (1 + 63 + 8)}
tmp[0] = 0x80
pad := int((55 - d.len) % 64) // calculate number of padding bytes
binary.little_endian_put_u64(mut tmp[1 + pad..], d.len << 3) // append length in bits
d.write(tmp[..1 + pad + 8]) or { panic(err) }
// The previous write ensures that a whole number of
// blocks (i.e. a multiple of 64 bytes) have been hashed.
if d.nx != 0 {
panic('d.nx != 0')
}
mut digest := []u8{len: size}
binary.little_endian_put_u32(mut digest, d.s[0])
binary.little_endian_put_u32(mut digest[4..], d.s[1])
binary.little_endian_put_u32(mut digest[8..], d.s[2])
binary.little_endian_put_u32(mut digest[12..], d.s[3])
return digest
}
// sum returns the MD5 checksum of the data.
pub fn sum(data []u8) []u8 {
mut d := new()
d.write(data) or { panic(err) }
return d.checksum()
}
fn block(mut dig Digest, p []u8) {
// For now just use block_generic until we have specific
// architecture optimized versions
block_generic(mut dig, p)
}
// size returns the size of the checksum in bytes.
pub fn (d &Digest) size() int {
return size
}
// block_size returns the block size of the checksum in bytes.
pub fn (d &Digest) block_size() int {
return block_size
}
// hexhash returns a hexadecimal MD5 hash sum `string` of `s`.
// Example: assert md5.hexhash('V') == '5206560a306a2e085a437fd258eb57ce'
pub fn hexhash(s string) string {
return sum(s.bytes()).hex()
}
+38
View File
@@ -0,0 +1,38 @@
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
import crypto.md5
import hash
// verify md5.Digest implements hash.Hash
fn test_digest_implements_hash() {
get_digest := fn () hash.Hash {
return md5.new()
}
}
fn test_crypto_md5() {
assert md5.sum('this is a md5 checksum.'.bytes()).hex() == '6fb421ff99036547655984da12973431'
}
fn test_crypto_md5_writer() {
mut digest := md5.new()
digest.write('this is a'.bytes()) or { assert false }
digest.write(' md5 checksum.'.bytes()) or { assert false }
mut sum := digest.sum([])
assert sum.hex() == '6fb421ff99036547655984da12973431'
sum = digest.sum([])
assert sum.hex() == '6fb421ff99036547655984da12973431'
}
fn test_crypto_md5_writer_reset() {
mut digest := md5.new()
digest.write('this is a'.bytes()) or { assert false }
digest.write(' md5 checksum.'.bytes()) or { assert false }
_ = digest.sum([])
digest.reset()
digest.write('this is a'.bytes()) or { assert false }
digest.write(' md5 checksum.'.bytes()) or { assert false }
sum := digest.sum([])
assert sum.hex() == '6fb421ff99036547655984da12973431'
}
+136
View File
@@ -0,0 +1,136 @@
// Copyright (c) 2019-2024 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
// This is the generic version with no architecture optimizations.
// In its own file so that an architecture
// optimized version can be substituted
module md5
import math.bits
@[direct_array_access; inline]
fn get_le_u32(b []u8, start int) u32 {
return u32(b[start]) | (u32(b[1 + start]) << u32(8)) | (u32(b[2 + start]) << u32(16)) | (u32(b[
3 + start]) << u32(24))
}
@[direct_array_access]
fn block_generic(mut dig Digest, p []u8) {
// load state
mut a := dig.s[0]
mut b := dig.s[1]
mut c := dig.s[2]
mut d := dig.s[3]
for i := 0; i <= p.len - block_size; i += block_size {
// save current state
aa := a
bb := b
cc := c
dd := d
// load input block
x0 := get_le_u32(p, 4 * 0x0 + i)
x1 := get_le_u32(p, 4 * 0x1 + i)
x2 := get_le_u32(p, 4 * 0x2 + i)
x3 := get_le_u32(p, 4 * 0x3 + i)
x4 := get_le_u32(p, 4 * 0x4 + i)
x5 := get_le_u32(p, 4 * 0x5 + i)
x6 := get_le_u32(p, 4 * 0x6 + i)
x7 := get_le_u32(p, 4 * 0x7 + i)
x8 := get_le_u32(p, 4 * 0x8 + i)
x9 := get_le_u32(p, 4 * 0x9 + i)
xa := get_le_u32(p, 4 * 0xa + i)
xb := get_le_u32(p, 4 * 0xb + i)
xc := get_le_u32(p, 4 * 0xc + i)
xd := get_le_u32(p, 4 * 0xd + i)
xe := get_le_u32(p, 4 * 0xe + i)
xf := get_le_u32(p, 4 * 0xf + i)
// round 1
a = b + bits.rotate_left_32((((c ^ d) & b) ^ d) + a + x0 + u32(0xd76aa478), 7)
d = a + bits.rotate_left_32((((b ^ c) & a) ^ c) + d + x1 + u32(0xe8c7b756), 12)
c = d + bits.rotate_left_32((((a ^ b) & d) ^ b) + c + x2 + u32(0x242070db), 17)
b = c + bits.rotate_left_32((((d ^ a) & c) ^ a) + b + x3 + u32(0xc1bdceee), 22)
a = b + bits.rotate_left_32((((c ^ d) & b) ^ d) + a + x4 + u32(0xf57c0faf), 7)
d = a + bits.rotate_left_32((((b ^ c) & a) ^ c) + d + x5 + u32(0x4787c62a), 12)
c = d + bits.rotate_left_32((((a ^ b) & d) ^ b) + c + x6 + u32(0xa8304613), 17)
b = c + bits.rotate_left_32((((d ^ a) & c) ^ a) + b + x7 + u32(0xfd469501), 22)
a = b + bits.rotate_left_32((((c ^ d) & b) ^ d) + a + x8 + u32(0x698098d8), 7)
d = a + bits.rotate_left_32((((b ^ c) & a) ^ c) + d + x9 + u32(0x8b44f7af), 12)
c = d + bits.rotate_left_32((((a ^ b) & d) ^ b) + c + xa + u32(0xffff5bb1), 17)
b = c + bits.rotate_left_32((((d ^ a) & c) ^ a) + b + xb + u32(0x895cd7be), 22)
a = b + bits.rotate_left_32((((c ^ d) & b) ^ d) + a + xc + u32(0x6b901122), 7)
d = a + bits.rotate_left_32((((b ^ c) & a) ^ c) + d + xd + u32(0xfd987193), 12)
c = d + bits.rotate_left_32((((a ^ b) & d) ^ b) + c + xe + u32(0xa679438e), 17)
b = c + bits.rotate_left_32((((d ^ a) & c) ^ a) + b + xf + u32(0x49b40821), 22)
// round 2
a = b + bits.rotate_left_32((((b ^ c) & d) ^ c) + a + x1 + u32(0xf61e2562), 5)
d = a + bits.rotate_left_32((((a ^ b) & c) ^ b) + d + x6 + u32(0xc040b340), 9)
c = d + bits.rotate_left_32((((d ^ a) & b) ^ a) + c + xb + u32(0x265e5a51), 14)
b = c + bits.rotate_left_32((((c ^ d) & a) ^ d) + b + x0 + u32(0xe9b6c7aa), 20)
a = b + bits.rotate_left_32((((b ^ c) & d) ^ c) + a + x5 + u32(0xd62f105d), 5)
d = a + bits.rotate_left_32((((a ^ b) & c) ^ b) + d + xa + u32(0x02441453), 9)
c = d + bits.rotate_left_32((((d ^ a) & b) ^ a) + c + xf + u32(0xd8a1e681), 14)
b = c + bits.rotate_left_32((((c ^ d) & a) ^ d) + b + x4 + u32(0xe7d3fbc8), 20)
a = b + bits.rotate_left_32((((b ^ c) & d) ^ c) + a + x9 + u32(0x21e1cde6), 5)
d = a + bits.rotate_left_32((((a ^ b) & c) ^ b) + d + xe + u32(0xc33707d6), 9)
c = d + bits.rotate_left_32((((d ^ a) & b) ^ a) + c + x3 + u32(0xf4d50d87), 14)
b = c + bits.rotate_left_32((((c ^ d) & a) ^ d) + b + x8 + u32(0x455a14ed), 20)
a = b + bits.rotate_left_32((((b ^ c) & d) ^ c) + a + xd + u32(0xa9e3e905), 5)
d = a + bits.rotate_left_32((((a ^ b) & c) ^ b) + d + x2 + u32(0xfcefa3f8), 9)
c = d + bits.rotate_left_32((((d ^ a) & b) ^ a) + c + x7 + u32(0x676f02d9), 14)
b = c + bits.rotate_left_32((((c ^ d) & a) ^ d) + b + xc + u32(0x8d2a4c8a), 20)
// round 3
a = b + bits.rotate_left_32((b ^ c ^ d) + a + x5 + u32(0xfffa3942), 4)
d = a + bits.rotate_left_32((a ^ b ^ c) + d + x8 + u32(0x8771f681), 11)
c = d + bits.rotate_left_32((d ^ a ^ b) + c + xb + u32(0x6d9d6122), 16)
b = c + bits.rotate_left_32((c ^ d ^ a) + b + xe + u32(0xfde5380c), 23)
a = b + bits.rotate_left_32((b ^ c ^ d) + a + x1 + u32(0xa4beea44), 4)
d = a + bits.rotate_left_32((a ^ b ^ c) + d + x4 + u32(0x4bdecfa9), 11)
c = d + bits.rotate_left_32((d ^ a ^ b) + c + x7 + u32(0xf6bb4b60), 16)
b = c + bits.rotate_left_32((c ^ d ^ a) + b + xa + u32(0xbebfbc70), 23)
a = b + bits.rotate_left_32((b ^ c ^ d) + a + xd + u32(0x289b7ec6), 4)
d = a + bits.rotate_left_32((a ^ b ^ c) + d + x0 + u32(0xeaa127fa), 11)
c = d + bits.rotate_left_32((d ^ a ^ b) + c + x3 + u32(0xd4ef3085), 16)
b = c + bits.rotate_left_32((c ^ d ^ a) + b + x6 + u32(0x04881d05), 23)
a = b + bits.rotate_left_32((b ^ c ^ d) + a + x9 + u32(0xd9d4d039), 4)
d = a + bits.rotate_left_32((a ^ b ^ c) + d + xc + u32(0xe6db99e5), 11)
c = d + bits.rotate_left_32((d ^ a ^ b) + c + xf + u32(0x1fa27cf8), 16)
b = c + bits.rotate_left_32((c ^ d ^ a) + b + x2 + u32(0xc4ac5665), 23)
// round 4
a = b + bits.rotate_left_32((c ^ (b | ~d)) + a + x0 + u32(0xf4292244), 6)
d = a + bits.rotate_left_32((b ^ (a | ~c)) + d + x7 + u32(0x432aff97), 10)
c = d + bits.rotate_left_32((a ^ (d | ~b)) + c + xe + u32(0xab9423a7), 15)
b = c + bits.rotate_left_32((d ^ (c | ~a)) + b + x5 + u32(0xfc93a039), 21)
a = b + bits.rotate_left_32((c ^ (b | ~d)) + a + xc + u32(0x655b59c3), 6)
d = a + bits.rotate_left_32((b ^ (a | ~c)) + d + x3 + u32(0x8f0ccc92), 10)
c = d + bits.rotate_left_32((a ^ (d | ~b)) + c + xa + u32(0xffeff47d), 15)
b = c + bits.rotate_left_32((d ^ (c | ~a)) + b + x1 + u32(0x85845dd1), 21)
a = b + bits.rotate_left_32((c ^ (b | ~d)) + a + x8 + u32(0x6fa87e4f), 6)
d = a + bits.rotate_left_32((b ^ (a | ~c)) + d + xf + u32(0xfe2ce6e0), 10)
c = d + bits.rotate_left_32((a ^ (d | ~b)) + c + x6 + u32(0xa3014314), 15)
b = c + bits.rotate_left_32((d ^ (c | ~a)) + b + xd + u32(0x4e0811a1), 21)
a = b + bits.rotate_left_32((c ^ (b | ~d)) + a + x4 + u32(0xf7537e82), 6)
d = a + bits.rotate_left_32((b ^ (a | ~c)) + d + xb + u32(0xbd3af235), 10)
c = d + bits.rotate_left_32((a ^ (d | ~b)) + c + x2 + u32(0x2ad7d2bb), 15)
b = c + bits.rotate_left_32((d ^ (c | ~a)) + b + x9 + u32(0xeb86d391), 21)
// add saved state
a += aa
b += bb
c += cc
d += dd
}
// save state
dig.s[0] = a
dig.s[1] = b
dig.s[2] = c
dig.s[3] = d
}