mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-02 12:48:14 +00:00
core/crypto/sha1: odinfmt (NFC)
This commit is contained in:
+77
-77
@@ -10,9 +10,9 @@ package sha1
|
|||||||
Implementation of the SHA1 hashing algorithm, as defined in RFC 3174 <https://datatracker.ietf.org/doc/html/rfc3174>
|
Implementation of the SHA1 hashing algorithm, as defined in RFC 3174 <https://datatracker.ietf.org/doc/html/rfc3174>
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import "core:io"
|
||||||
import "core:mem"
|
import "core:mem"
|
||||||
import "core:os"
|
import "core:os"
|
||||||
import "core:io"
|
|
||||||
|
|
||||||
import "../util"
|
import "../util"
|
||||||
|
|
||||||
@@ -25,77 +25,77 @@ DIGEST_SIZE :: 20
|
|||||||
// hash_string will hash the given input and return the
|
// hash_string will hash the given input and return the
|
||||||
// computed hash
|
// computed hash
|
||||||
hash_string :: proc(data: string) -> [DIGEST_SIZE]byte {
|
hash_string :: proc(data: string) -> [DIGEST_SIZE]byte {
|
||||||
return hash_bytes(transmute([]byte)(data))
|
return hash_bytes(transmute([]byte)(data))
|
||||||
}
|
}
|
||||||
|
|
||||||
// hash_bytes will hash the given input and return the
|
// hash_bytes will hash the given input and return the
|
||||||
// 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: Sha1_Context
|
ctx: Sha1_Context
|
||||||
init(&ctx)
|
init(&ctx)
|
||||||
update(&ctx, data)
|
update(&ctx, data)
|
||||||
final(&ctx, hash[:])
|
final(&ctx, hash[:])
|
||||||
return hash
|
return hash
|
||||||
}
|
}
|
||||||
|
|
||||||
// hash_string_to_buffer will hash the given input and assign the
|
// hash_string_to_buffer will hash the given input and assign the
|
||||||
// computed hash to the second parameter.
|
// computed hash to 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_string_to_buffer :: proc(data: string, hash: []byte) {
|
hash_string_to_buffer :: proc(data: string, hash: []byte) {
|
||||||
hash_bytes_to_buffer(transmute([]byte)(data), hash)
|
hash_bytes_to_buffer(transmute([]byte)(data), hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
// hash_bytes_to_buffer will hash the given input and write the
|
// hash_bytes_to_buffer will hash the given input and write the
|
||||||
// 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) {
|
||||||
assert(len(hash) >= DIGEST_SIZE, "Size of destination buffer is smaller than the digest size")
|
assert(len(hash) >= DIGEST_SIZE, "Size of destination buffer is smaller than the digest size")
|
||||||
ctx: Sha1_Context
|
ctx: Sha1_Context
|
||||||
init(&ctx)
|
init(&ctx)
|
||||||
update(&ctx, data)
|
update(&ctx, data)
|
||||||
final(&ctx, hash)
|
final(&ctx, hash)
|
||||||
}
|
}
|
||||||
|
|
||||||
// hash_stream will read the stream in chunks and compute a
|
// hash_stream will read the stream in chunks and compute a
|
||||||
// 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: Sha1_Context
|
ctx: Sha1_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)
|
||||||
if read > 0 {
|
if read > 0 {
|
||||||
update(&ctx, buf[:read])
|
update(&ctx, buf[:read])
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
final(&ctx, hash[:])
|
final(&ctx, hash[:])
|
||||||
return hash, true
|
return hash, true
|
||||||
}
|
}
|
||||||
|
|
||||||
// hash_file will read the file provided by the given handle
|
// hash_file will read the file provided by the given handle
|
||||||
// and compute a hash
|
// and compute a hash
|
||||||
hash_file :: proc(hd: os.Handle, load_at_once := false) -> ([DIGEST_SIZE]byte, bool) {
|
hash_file :: proc(hd: os.Handle, load_at_once := false) -> ([DIGEST_SIZE]byte, bool) {
|
||||||
if !load_at_once {
|
if !load_at_once {
|
||||||
return hash_stream(os.stream_from_handle(hd))
|
return hash_stream(os.stream_from_handle(hd))
|
||||||
} else {
|
} else {
|
||||||
if buf, ok := os.read_entire_file(hd); ok {
|
if buf, ok := os.read_entire_file(hd); ok {
|
||||||
return hash_bytes(buf[:]), ok
|
return hash_bytes(buf[:]), ok
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return [DIGEST_SIZE]byte{}, false
|
return [DIGEST_SIZE]byte{}, false
|
||||||
}
|
}
|
||||||
|
|
||||||
hash :: proc {
|
hash :: proc {
|
||||||
hash_stream,
|
hash_stream,
|
||||||
hash_file,
|
hash_file,
|
||||||
hash_bytes,
|
hash_bytes,
|
||||||
hash_string,
|
hash_string,
|
||||||
hash_bytes_to_buffer,
|
hash_bytes_to_buffer,
|
||||||
hash_string_to_buffer,
|
hash_string_to_buffer,
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@@ -108,10 +108,10 @@ init :: proc(ctx: ^Sha1_Context) {
|
|||||||
ctx.state[2] = 0x98badcfe
|
ctx.state[2] = 0x98badcfe
|
||||||
ctx.state[3] = 0x10325476
|
ctx.state[3] = 0x10325476
|
||||||
ctx.state[4] = 0xc3d2e1f0
|
ctx.state[4] = 0xc3d2e1f0
|
||||||
ctx.k[0] = 0x5a827999
|
ctx.k[0] = 0x5a827999
|
||||||
ctx.k[1] = 0x6ed9eba1
|
ctx.k[1] = 0x6ed9eba1
|
||||||
ctx.k[2] = 0x8f1bbcdc
|
ctx.k[2] = 0x8f1bbcdc
|
||||||
ctx.k[3] = 0xca62c1d6
|
ctx.k[3] = 0xca62c1d6
|
||||||
}
|
}
|
||||||
|
|
||||||
update :: proc(ctx: ^Sha1_Context, data: []byte) {
|
update :: proc(ctx: ^Sha1_Context, data: []byte) {
|
||||||
@@ -131,24 +131,23 @@ final :: proc(ctx: ^Sha1_Context, hash: []byte) {
|
|||||||
|
|
||||||
if ctx.datalen < 56 {
|
if ctx.datalen < 56 {
|
||||||
ctx.data[i] = 0x80
|
ctx.data[i] = 0x80
|
||||||
i += 1
|
i += 1
|
||||||
for i < 56 {
|
for i < 56 {
|
||||||
ctx.data[i] = 0x00
|
ctx.data[i] = 0x00
|
||||||
i += 1
|
i += 1
|
||||||
}
|
}
|
||||||
}
|
} else {
|
||||||
else {
|
|
||||||
ctx.data[i] = 0x80
|
ctx.data[i] = 0x80
|
||||||
i += 1
|
i += 1
|
||||||
for i < BLOCK_SIZE {
|
for i < BLOCK_SIZE {
|
||||||
ctx.data[i] = 0x00
|
ctx.data[i] = 0x00
|
||||||
i += 1
|
i += 1
|
||||||
}
|
}
|
||||||
transform(ctx, ctx.data[:])
|
transform(ctx, ctx.data[:])
|
||||||
mem.set(&ctx.data, 0, 56)
|
mem.set(&ctx.data, 0, 56)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.bitlen += u64(ctx.datalen * 8)
|
ctx.bitlen += u64(ctx.datalen * 8)
|
||||||
ctx.data[63] = u8(ctx.bitlen)
|
ctx.data[63] = u8(ctx.bitlen)
|
||||||
ctx.data[62] = u8(ctx.bitlen >> 8)
|
ctx.data[62] = u8(ctx.bitlen >> 8)
|
||||||
ctx.data[61] = u8(ctx.bitlen >> 16)
|
ctx.data[61] = u8(ctx.bitlen >> 16)
|
||||||
@@ -160,9 +159,9 @@ final :: proc(ctx: ^Sha1_Context, hash: []byte) {
|
|||||||
transform(ctx, ctx.data[:])
|
transform(ctx, ctx.data[:])
|
||||||
|
|
||||||
for j: u32 = 0; j < 4; j += 1 {
|
for j: u32 = 0; j < 4; j += 1 {
|
||||||
hash[j] = u8(ctx.state[0] >> (24 - j * 8)) & 0x000000ff
|
hash[j] = u8(ctx.state[0] >> (24 - j * 8)) & 0x000000ff
|
||||||
hash[j + 4] = u8(ctx.state[1] >> (24 - j * 8)) & 0x000000ff
|
hash[j + 4] = u8(ctx.state[1] >> (24 - j * 8)) & 0x000000ff
|
||||||
hash[j + 8] = u8(ctx.state[2] >> (24 - j * 8)) & 0x000000ff
|
hash[j + 8] = u8(ctx.state[2] >> (24 - j * 8)) & 0x000000ff
|
||||||
hash[j + 12] = u8(ctx.state[3] >> (24 - j * 8)) & 0x000000ff
|
hash[j + 12] = u8(ctx.state[3] >> (24 - j * 8)) & 0x000000ff
|
||||||
hash[j + 16] = u8(ctx.state[4] >> (24 - j * 8)) & 0x000000ff
|
hash[j + 16] = u8(ctx.state[4] >> (24 - j * 8)) & 0x000000ff
|
||||||
}
|
}
|
||||||
@@ -172,28 +171,29 @@ final :: proc(ctx: ^Sha1_Context, hash: []byte) {
|
|||||||
SHA1 implementation
|
SHA1 implementation
|
||||||
*/
|
*/
|
||||||
|
|
||||||
BLOCK_SIZE :: 64
|
BLOCK_SIZE :: 64
|
||||||
|
|
||||||
Sha1_Context :: struct {
|
Sha1_Context :: struct {
|
||||||
data: [BLOCK_SIZE]byte,
|
data: [BLOCK_SIZE]byte,
|
||||||
datalen: u32,
|
datalen: u32,
|
||||||
bitlen: u64,
|
bitlen: u64,
|
||||||
state: [5]u32,
|
state: [5]u32,
|
||||||
k: [4]u32,
|
k: [4]u32,
|
||||||
}
|
}
|
||||||
|
|
||||||
transform :: proc(ctx: ^Sha1_Context, data: []byte) {
|
transform :: proc(ctx: ^Sha1_Context, data: []byte) {
|
||||||
a, b, c, d, e, i, j, t: u32
|
a, b, c, d, e, i, j, t: u32
|
||||||
m: [80]u32
|
m: [80]u32
|
||||||
|
|
||||||
for i, j = 0, 0; i < 16; i += 1 {
|
for i, j = 0, 0; i < 16; i += 1 {
|
||||||
m[i] = u32(data[j]) << 24 + u32(data[j + 1]) << 16 + u32(data[j + 2]) << 8 + u32(data[j + 3])
|
m[i] =
|
||||||
j += 4
|
u32(data[j]) << 24 + u32(data[j + 1]) << 16 + u32(data[j + 2]) << 8 + u32(data[j + 3])
|
||||||
}
|
j += 4
|
||||||
|
}
|
||||||
for i < 80 {
|
for i < 80 {
|
||||||
m[i] = (m[i - 3] ~ m[i - 8] ~ m[i - 14] ~ m[i - 16])
|
m[i] = (m[i - 3] ~ m[i - 8] ~ m[i - 14] ~ m[i - 16])
|
||||||
m[i] = (m[i] << 1) | (m[i] >> 31)
|
m[i] = (m[i] << 1) | (m[i] >> 31)
|
||||||
i += 1
|
i += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
a = ctx.state[0]
|
a = ctx.state[0]
|
||||||
@@ -217,7 +217,7 @@ transform :: proc(ctx: ^Sha1_Context, data: []byte) {
|
|||||||
c = util.ROTL32(b, 30)
|
c = util.ROTL32(b, 30)
|
||||||
b = a
|
b = a
|
||||||
a = t
|
a = t
|
||||||
i += 1
|
i += 1
|
||||||
}
|
}
|
||||||
for i < 60 {
|
for i < 60 {
|
||||||
t = util.ROTL32(a, 5) + ((b & c) ~ (b & d) ~ (c & d)) + e + ctx.k[2] + m[i]
|
t = util.ROTL32(a, 5) + ((b & c) ~ (b & d) ~ (c & d)) + e + ctx.k[2] + m[i]
|
||||||
@@ -226,7 +226,7 @@ transform :: proc(ctx: ^Sha1_Context, data: []byte) {
|
|||||||
c = util.ROTL32(b, 30)
|
c = util.ROTL32(b, 30)
|
||||||
b = a
|
b = a
|
||||||
a = t
|
a = t
|
||||||
i += 1
|
i += 1
|
||||||
}
|
}
|
||||||
for i < 80 {
|
for i < 80 {
|
||||||
t = util.ROTL32(a, 5) + (b ~ c ~ d) + e + ctx.k[3] + m[i]
|
t = util.ROTL32(a, 5) + (b ~ c ~ d) + e + ctx.k[3] + m[i]
|
||||||
@@ -235,7 +235,7 @@ transform :: proc(ctx: ^Sha1_Context, data: []byte) {
|
|||||||
c = util.ROTL32(b, 30)
|
c = util.ROTL32(b, 30)
|
||||||
b = a
|
b = a
|
||||||
a = t
|
a = t
|
||||||
i += 1
|
i += 1
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.state[0] += a
|
ctx.state[0] += a
|
||||||
|
|||||||
Reference in New Issue
Block a user