Browse Source

core/crypto/sha1: API cleanup

-sha1.Sha1_Context -> Context
Yawning Angel 1 year ago
parent
commit
506adfb105
2 changed files with 28 additions and 29 deletions
  1. 10 8
      core/crypto/sha1/sha1.odin
  2. 18 21
      vendor/botan/sha1/sha1.odin

+ 10 - 8
core/crypto/sha1/sha1.odin

@@ -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: Sha1_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: Sha1_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: Sha1_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: ^Sha1_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
@@ -118,7 +120,7 @@ init :: proc(ctx: ^Sha1_Context) {
 	ctx.is_initialized = true
 	ctx.is_initialized = true
 }
 }
 
 
-update :: proc(ctx: ^Sha1_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 {
@@ -132,7 +134,7 @@ update :: proc(ctx: ^Sha1_Context, data: []byte) {
 	}
 	}
 }
 }
 
 
-final :: proc(ctx: ^Sha1_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 {
@@ -176,7 +178,7 @@ final :: proc(ctx: ^Sha1_Context, hash: []byte) {
 
 
 BLOCK_SIZE :: 64
 BLOCK_SIZE :: 64
 
 
-Sha1_Context :: struct {
+Context :: struct {
 	data:    [BLOCK_SIZE]byte,
 	data:    [BLOCK_SIZE]byte,
 	datalen: u32,
 	datalen: u32,
 	bitlen:  u64,
 	bitlen:  u64,
@@ -187,7 +189,7 @@ Sha1_Context :: struct {
 }
 }
 
 
 @(private)
 @(private)
-transform :: proc "contextless" (ctx: ^Sha1_Context, data: []byte) {
+transform :: proc "contextless" (ctx: ^Context, data: []byte) {
 	a, b, c, d, e, i, t: u32
 	a, b, c, d, e, i, t: u32
 	m: [80]u32
 	m: [80]u32
 
 

+ 18 - 21
vendor/botan/sha1/sha1.odin

@@ -32,11 +32,10 @@ hash_string :: proc "contextless" (data: string) -> [DIGEST_SIZE]byte {
 // computed hash
 // computed hash
 hash_bytes :: proc "contextless" (data: []byte) -> [DIGEST_SIZE]byte {
 hash_bytes :: proc "contextless" (data: []byte) -> [DIGEST_SIZE]byte {
     hash: [DIGEST_SIZE]byte
     hash: [DIGEST_SIZE]byte
-    ctx: botan.hash_t
-    botan.hash_init(&ctx, botan.HASH_SHA1, 0)
-    botan.hash_update(ctx, len(data) == 0 ? nil : &data[0], uint(len(data)))
-    botan.hash_final(ctx, &hash[0])
-    botan.hash_destroy(ctx)
+    ctx: Context
+    init(&ctx)
+    update(&ctx, data)
+    final(&ctx, hash[:])
     return hash
     return hash
 }
 }
 
 
@@ -52,31 +51,29 @@ hash_string_to_buffer :: proc(data: string, hash: []byte) {
 // 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: botan.hash_t
-    botan.hash_init(&ctx, botan.HASH_SHA1, 0)
-    botan.hash_update(ctx, len(data) == 0 ? nil : &data[0], uint(len(data)))
-    botan.hash_final(ctx, &hash[0])
-    botan.hash_destroy(ctx)
+    ctx: Context
+    init(&ctx)
+    update(&ctx, data)
+    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: botan.hash_t
-    botan.hash_init(&ctx, botan.HASH_SHA1, 0)
+    ctx: Context
+    init(&ctx)
     buf := make([]byte, 512)
     buf := make([]byte, 512)
     defer delete(buf)
     defer delete(buf)
     i := 1
     i := 1
     for i > 0 {
     for i > 0 {
         i, _ = io.read(s, buf)
         i, _ = io.read(s, buf)
         if i > 0 {
         if i > 0 {
-            botan.hash_update(ctx, len(buf) == 0 ? nil : &buf[0], uint(i))
-        } 
+            update(&ctx, buf[:i])
+        }
     }
     }
-    botan.hash_final(ctx, &hash[0])
-    botan.hash_destroy(ctx)
-    return hash, true 
+    final(&ctx, hash[:])
+    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
@@ -105,17 +102,17 @@ hash :: proc {
     Low level API
     Low level API
 */
 */
 
 
-Sha1_Context :: botan.hash_t
+Context :: botan.hash_t
 
 
-init :: proc "contextless" (ctx: ^botan.hash_t) {
+init :: proc "contextless" (ctx: ^Context) {
     botan.hash_init(ctx, botan.HASH_SHA1, 0)
     botan.hash_init(ctx, botan.HASH_SHA1, 0)
 }
 }
 
 
-update :: proc "contextless" (ctx: ^botan.hash_t, data: []byte) {
+update :: proc "contextless" (ctx: ^Context, data: []byte) {
     botan.hash_update(ctx^, len(data) == 0 ? nil : &data[0], uint(len(data)))
     botan.hash_update(ctx^, len(data) == 0 ? nil : &data[0], uint(len(data)))
 }
 }
 
 
-final :: proc "contextless" (ctx: ^botan.hash_t, hash: []byte) {
+final :: proc "contextless" (ctx: ^Context, hash: []byte) {
     botan.hash_final(ctx^, &hash[0])
     botan.hash_final(ctx^, &hash[0])
     botan.hash_destroy(ctx^)
     botan.hash_destroy(ctx^)
 }
 }