douxian9706 2016-08-08 12:36
浏览 82
已采纳

加密/ sha256未声明名称中的GetSha224

I just startet with golang and write a very small script, everything works except for sha224. Can someone explain me please why i get the error (in regards to my script)? i dont need this error fixed, i wanna understand it to prevent future errors from my side.

The error 49:12: undeclared name: sha224 indicates that i didnt declare something, but i cant find it because i did the same for all hashing algorythms.

Im not a programmer, nor a specialist in golang. Its about my 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 ...
", 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
", html)
    toHash := CToGoString(html[:])

    fmt.Printf("%s
", GetMD5Hash(toHash))
    fmt.Printf("%s
", GetSha1Hash(toHash))
    fmt.Printf("%s
", GetSha224Hash(toHash))
    fmt.Printf("%s
", GetSha256Hash(toHash))
    fmt.Printf("%s
", 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
    for i, b := range c {
        if b == 0 {
            break
        }
        n = i
    }
    return string(c[:n+1])
}
  • 写回答

1条回答 默认 最新

  • dop83362 2016-08-08 12:40
    关注

    There is no sha224 package, the SHA224 algorithm is implemented in the crypto/sha256 package (along with SHA256 of course). This is because SHA224 is simply a truncated version of SHA256.

    You properly imported that, use the sha256.New224() function to acquire a 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 you just want to hash some data, you don't need to create a hasher. Most packages provide a shortcut to do that, e.g. sha256.Sum224():

    func GetSha224Hash(text string) string {
        s := sha256.Sum224([]byte(text))
        return hex.EncodeToString(s[:])
    }
    

    Or here's a one-liner:

    func GetSha224Hash(text string) string {
        return fmt.Sprintf("%x", sha256.Sum224([]byte(text)))
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机