Skip to content

Instantly share code, notes, and snippets.

@sierrezinal
Forked from miguelmota/crypto.go
Created August 31, 2021 23:32
Show Gist options
  • Save sierrezinal/3a11c158449be370305d18a9df73340d to your computer and use it in GitHub Desktop.
Save sierrezinal/3a11c158449be370305d18a9df73340d to your computer and use it in GitHub Desktop.

Revisions

  1. @miguelmota miguelmota revised this gist Jun 5, 2019. 1 changed file with 12 additions and 0 deletions.
    12 changes: 12 additions & 0 deletions main.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,12 @@
    package main

    import (
    "crypto/sha256"
    "fmt"
    )

    func main() {
    data := []byte("hello")
    hash := sha256.Sum256(data)
    fmt.Printf("%x", hash[:]) // 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
    }
  2. @miguelmota miguelmota revised this gist Dec 17, 2018. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion crypto_test.go
    Original file line number Diff line number Diff line change
    @@ -12,8 +12,8 @@ func TestNewSHA256(t *testing.T) {
    out string
    }{
    {[]byte(""), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
    {[]byte("hello"), "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"},
    {[]byte("abc"), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"},
    {[]byte("hello"), "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"},
    } {
    t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
    result := NewSHA256(tt.in)
  3. @miguelmota miguelmota created this gist Dec 17, 2018.
    11 changes: 11 additions & 0 deletions crypto.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,11 @@
    package crypto

    import (
    "crypto/sha256"
    )

    // NewSHA256 ...
    func NewSHA256(data []byte) []byte {
    hash := sha256.Sum256(data)
    return hash[:]
    }
    26 changes: 26 additions & 0 deletions crypto_test.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,26 @@
    package crypto

    import (
    "encoding/hex"
    "fmt"
    "testing"
    )

    func TestNewSHA256(t *testing.T) {
    for i, tt := range []struct {
    in []byte
    out string
    }{
    {[]byte(""), "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"},
    {[]byte("hello"), "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824"},
    {[]byte("abc"), "ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"},
    } {
    t.Run(fmt.Sprintf("%v", i), func(t *testing.T) {
    result := NewSHA256(tt.in)
    if hex.EncodeToString(result) != tt.out {
    t.Errorf("want %v; got %v", tt.out, hex.EncodeToString(result))
    }
    })
    }
    }