golang MD5

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了golang MD5相关的知识,希望对你有一定的参考价值。

package main

// Source: https://www.socketloop.com/tutorials/how-to-generate-checksum-for-file-in-go

import (
	"crypto/md5"
	"fmt"
	"io"
	"math"
	"os"
)

// 8KB
const filechunk = 8192

func main() {
	// Ensure the file argument is passed
	if len(os.Args) != 2 {
		fmt.Println("Please use this syntax: md5.exe file.txt")
		return
	}

	// Open the file for reading
	file, err := os.Open(os.Args[1])
	if err != nil {
		fmt.Println("Cannot find file:", os.Args[1])
		return
	}

	defer file.Close()

	// Get file info
	info, err := file.Stat()
	if err != nil {
		fmt.Println("Cannot access file:", os.Args[1])
		return
	}

	// Get the filesize
	filesize := info.Size()

	// Calculate the number of blocks
	blocks := uint64(math.Ceil(float64(filesize) / float64(filechunk)))

	// Start hash
	hash := md5.New()

	// Check each block
	for i := uint64(0); i < blocks; i++ {
		// Calculate block size
		blocksize := int(math.Min(filechunk, float64(filesize-int64(i*filechunk))))

		// Make a buffer
		buf := make([]byte, blocksize)

		// Make a buffer
		file.Read(buf)

		// Write to the buffer
		io.WriteString(hash, string(buf))
	}

	// Output the results
	fmt.Printf("%x\n", hash.Sum(nil))
}

golang go_md5

func Md5Middle() gin.HandlerFunc {
    return func(c *gin.Context){
        name := c.PostForm("name")
        password := c.PostForm("password")
        md5Ctx := md5.New()
        md5Ctx.Write([]byte(password))
        password = hex.EncodeToString(md5Ctx.Sum([]byte(name + "aabbcc")))
        c.Set("name",name)
        c.Set("password",password)
        c.Next()
    }
}
package main

import (
	"fmt"
	"golang.org/x/crypto/bcrypt"
)

func main() {
	pass := "qwer1234"
	hash, err := HashAndSalt([]byte(pass))
	if err != nil {
		panic(err)
	}
	fmt.Println(hash)
	has := "$2a$10$rPAa8SQpxCl5z1FiubT77uFuPEGqm2oPDDp9NoHY8JBi9RWHwV/PC"
	flag := ComparePasswords(has, pass)
	fmt.Println(flag)
}

func HashAndSalt(pwd []byte) (string, error) {

	// Use GenerateFromPassword to hash & salt pwd
	// MinCost is just an integer constant provided by the bcrypt
	// package along with DefaultCost & MaxCost.
	// The cost can be any value you want provided it isn't lower
	// than the MinCost (4)
	hash, err := bcrypt.GenerateFromPassword(pwd, bcrypt.DefaultCost)
	if err != nil {
		return "", err
	}
	// GenerateFromPassword returns a byte slice so we need to
	// convert the bytes to a string and return it
	return string(hash), nil
}

func ComparePasswords(hashedPwd, plainPwd string) bool {
	// Since we'll be getting the hashed password from the DB it
	// will be a string so we'll need to convert it to a byte slice
	byteHash := []byte(hashedPwd)
	err := bcrypt.CompareHashAndPassword(byteHash, []byte(plainPwd))
	if err != nil {
		return false
	}

	return true
}

以上是关于golang MD5的主要内容,如果未能解决你的问题,请参考以下文章

golang Golang - 如何使用MD5散列字符串。

golang Golang - 如何使用MD5散列字符串。

golang Golang - 如何使用MD5散列字符串。

golang MD5

golang 中的md5 hmacsha1算法的简单实现

Golang语言之JSON md5