Go语言学习#3-加解密、哈希操作


=Start=

缘由:

最近在跟着郝林老师的《Go语言核心36讲》这门课程重拾Go语言学习(之前在15年的时候我自学过一段时间,不过后来没怎么用Go来写代码,所以有些内容都忘了,这次希望能坚持下去),所以准备把一些常见功能用Go语言试着实践一遍,加强学习效果和对这门语言的感觉。

正文:

参考解答:
Go 语言中如何对字符串进行SHA-1/MD5等Hash操作?
package main

import (
    "crypto/sha1"
    "encoding/hex"
    "fmt"
    "io"
)

func main() {
    // 不论是sha1还是md5,求Hash都是先 New() 然后再 Write() 最后 Sum()
    s := "hash this string"
    h := sha1.New()
    h.Write([]byte(s))

    fmt.Printf("%x\n", h.Sum(nil))

    // sha1_hash := hex.EncodeToString(h.Sum(nil))
    // fmt.Println(sha1_hash)
    fmt.Println(hex.EncodeToString(h.Sum(nil)))


    // 使用 io.WriteString 将字符串写入,而不是用 []byte(string_here) 进行转换
    h5 := sha1.New()
    io.WriteString(h5, "hash this string")
    fmt.Printf("%x\n", h5.Sum(nil))
}
Go 语言中如何对文件进行SHA-1/MD5等Hash操作?
package main

import (
    "crypto/sha1"
    "fmt"
    "io"
    "log"
    "os"
)

func main() {
    f, err := os.Open("file.txt")
    if err != nil {
        log.Fatal(err)
    }
    defer f.Close()

    h := sha1.New()
    if _, err := io.Copy(h, f); err != nil {
        log.Fatal(err)
    }

    fmt.Printf("%x", h.Sum(nil))

}
Go 语言中如何对字符串进行AES加解密?
package main

import (
    "crypto/aes"
    "crypto/cipher"
    "crypto/rand"
    "encoding/base64"
    "errors"
    "fmt"
    "io"
    "log"
)

func main() {
    key := []byte("a very very very very secret key") // 32 bytes
    plaintext := []byte("some really really really long plaintext")
    fmt.Printf("%s\n", plaintext)
    ciphertext, err := encrypt(key, plaintext)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%0x\n", ciphertext)
    result, err := decrypt(key, ciphertext)
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("%s\n", result)
}

// See alternate IV creation from ciphertext below
//var iv = []byte{35, 46, 57, 24, 85, 35, 24, 74, 87, 35, 88, 98, 66, 32, 14, 05}

func encrypt(key, text []byte) ([]byte, error) {
    block, err := aes.NewCipher(key)
    if err != nil {
        return nil, err
    }
    b := base64.StdEncoding.EncodeToString(text)
    ciphertext := make([]byte, aes.BlockSize+len(b))
    iv := ciphertext[:aes.BlockSize]
    if _, err := io.ReadFull(rand.Reader, iv); err != nil {
        return nil, err
    }
    cfb := cipher.NewCFBEncrypter(block, iv)
    cfb.XORKeyStream(ciphertext[aes.BlockSize:], []byte(b))
    return ciphertext, nil
}

func decrypt(key, text []byte) ([]byte, error) {
    block, err := aes.NewCipher(key)
    if err != nil {
        return nil, err
    }
    if len(text) < aes.BlockSize {
        return nil, errors.New("ciphertext too short")
    }
    iv := text[:aes.BlockSize]
    text = text[aes.BlockSize:]
    cfb := cipher.NewCFBDecrypter(block, iv)
    cfb.XORKeyStream(text, text)
    data, err := base64.StdEncoding.DecodeString(string(text))
    if err != nil {
        return nil, err
    }
    return data, nil
}

 

Go 语言中如何对字符串进行base64编解码?
  • func (enc *Encoding) EncodeToString(src []byte) string
  • func (enc *Encoding) DecodeString(s string) ([]byte, error)
package main

import (
    "encoding/base64"
    "fmt"
)

func main() {
    msg := "Hello, 世界"
    // base64 编码
    encoded := base64.StdEncoding.EncodeToString([]byte(msg))
    fmt.Println(encoded) // SGVsbG8sIOS4lueVjA==

    // base64 解码
    decoded, err := base64.StdEncoding.DecodeString(encoded)
    if err != nil {
        fmt.Println("decode error:", err)
        return
    }
    fmt.Println(string(decoded)) // Hello, 世界

}

 

参考链接:

=END=

, ,

《“Go语言学习#3-加解密、哈希操作”》 有 1 条评论

回复 hi 取消回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注