Skip to content

Instantly share code, notes, and snippets.

@VMuliadi
Last active February 5, 2020 10:59
Show Gist options
  • Save VMuliadi/c4b86f3ab43dba85995f1525c41c81d2 to your computer and use it in GitHub Desktop.
Save VMuliadi/c4b86f3ab43dba85995f1525c41c81d2 to your computer and use it in GitHub Desktop.
Get SHA256 from file in golang
package main
import (
"bufio"
"crypto/sha256"
"encoding/hex"
"fmt"
"os"
)
func main() {
uploadedFile, _ := os.Open(filename)
fileStat, _ := uploadedFile.Stat()
bytes := make([]byte, fileStat.Size())
buffer := bufio.NewReader(uploadedFile)
_, err := buffer.Read(bytes)
if err != nil {
panic(err)
}
sha256sumtext := sha256.Sum256(bytes)
fmt.Printf("%s", hex.EncodeToString(sha256sumtext[:]))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment