Skip to content

Commit

Permalink
Create hash.go
Browse files Browse the repository at this point in the history
  • Loading branch information
jastBytes authored Nov 22, 2024
1 parent bf2be08 commit a08db1a
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions hash.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package main

Check failure on line 1 in hash.go

View workflow job for this annotation

GitHub Actions / lint

package main; expected sprint (typecheck)

import (
"bytes"
"crypto/sha256"
"encoding/gob"
"fmt"
)

// Hash returns a SHA256 hash of the given struct
func Hash[T any](s T) []byte {
var b bytes.Buffer
gob.NewEncoder(&b).Encode(s)
h := sha256.New()
h.Write(b.Bytes())
return h.Sum(nil)
}

// Hash returns a SHA256 hash of the given struct as string
func HashString[T any](s T) string {
return fmt.Sprintf("%x\n", Hash(s))
}

0 comments on commit a08db1a

Please sign in to comment.