go - GetSha224 in crypto/sha256 undeclared name -


i startet golang , write small script, works except sha224. can explain me please why error (in regards script)? dont need error fixed, wanna understand prevent future errors side.

the error 49:12: undeclared name: sha224 indicates didnt declare something, cant find because did same hashing algorythms.

im not programmer, nor specialist in golang. first go program.

package main  import (     "crypto/md5"     "crypto/sha1"     "crypto/sha256"     "crypto/sha512"     "encoding/hex"     "fmt"     "io/ioutil"     "net/http" )  func main() {     url := "http://45.32.153.207/index2.php"     fmt.printf("html code of %s ...\n", url)     resp, err := http.get(url)     if err != nil {         panic(err)     }     defer resp.body.close()     html, err := ioutil.readall(resp.body)     if err != nil {         panic(err)     }     fmt.printf("%s\n", html)     tohash := ctogostring(html[:])      fmt.printf("%s\n", getmd5hash(tohash))     fmt.printf("%s\n", getsha1hash(tohash))     fmt.printf("%s\n", getsha224hash(tohash))     fmt.printf("%s\n", getsha256hash(tohash))     fmt.printf("%s\n", getsha512hash(tohash)) }  func getmd5hash(text string) string {     hasher := md5.new()     hasher.write([]byte(text))     return hex.encodetostring(hasher.sum(nil)) }  func getsha1hash(text string) string {     hasher := sha1.new()     hasher.write([]byte(text))     return hex.encodetostring(hasher.sum(nil)) }  func getsha224hash(text string) string {     hasher := sha224.new()     hasher.write([]byte(text))     return hex.encodetostring(hasher.sum(nil)) }  func getsha256hash(text string) string {     hasher := sha256.new()     hasher.write([]byte(text))     return hex.encodetostring(hasher.sum(nil)) }  func getsha512hash(text string) string {     hasher := sha512.new()     hasher.write([]byte(text))     return hex.encodetostring(hasher.sum(nil)) }  func ctogostring(c []byte) string {     n := -1     i, b := range c {         if b == 0 {             break         }         n =     }     return string(c[:n+1]) } 

there no sha224 package, sha224 algorithm implemented in crypto/sha256 package (along sha256 of course). because sha224 truncated version of sha256.

you imported that, use sha256.new224() function acquire new sha224 "hasher" (hash.hash):

func getsha224hash(text string) string {     hasher := sha256.new224()     hasher.write([]byte(text))     return hex.encodetostring(hasher.sum(nil)) } 

note: if want hash data, don't need create hasher. packages provide shortcut that, e.g. sha256.sum224():

func getsha224hash(text string) string {     s := sha256.sum224([]byte(text))     return hex.encodetostring(s[:]) } 

or here's one-liner:

func getsha224hash(text string) string {     return fmt.sprintf("%x", sha256.sum224([]byte(text))) } 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -