身份验证期间的缓冲

huangapple go评论77阅读模式
英文:

buffering during authentication

问题

在这本关于使用Go构建Web应用程序的书籍中,它演示了如何创建自定义身份验证。注册功能的一部分使用了以下代码:

md5Password := md5.New()
io.WriteString(md5Password, password)
buffer := bytes.NewBuffer(nil)
fmt.Fprintf(buffer, "%x", md5Password.Sum(nil))
newPass := buffer.String()

这段代码基本上在登录功能中重复出现:

md5Password := md5.New()
io.WriteString(md5Password, password)
buffer := bytes.NewBuffer(nil)
fmt.Fprintf(buffer, "%x", md5Password.Sum(nil))
newPass := buffer.String()

在代码的最后四行中,缓冲区的作用是什么?为什么它很重要,缓冲如何实现这一目的?

英文:

In this book enter link description here on building web applications with Go, it demonstrates how to create custom authentication. Part of the registration function uses this code

md5Password := md5.New()
io.WriteString(md5Password, password)
buffer := bytes.NewBuffer(nil)
fmt.Fprintf(buffer, "%x", md5Password.Sum(nil))
newPass := buffer.String()

which is basically repeated in the login function

md5Password := md5.New()
io.WriteString(md5Password, password)
buffer := bytes.NewBuffer(nil)
fmt.Fprintf(buffer, "%x", md5Password.Sum(nil))
newPass := buffer.String()

What is happening with the buffering in the last 4 lines of the code? Why is it important and how does buffering achieve it?

答案1

得分: 0

我不确定这样做的目的是什么,但是可以用以下代码替换:

md5Password := md5.New()
io.WriteString(md5Password, password)
newPass := fmt.Sprintf("%x", md5Password.Sum(nil))

原始的书可能已经过时了,但是没有必要进行额外的 bytes.Buffer 步骤。

英文:

I'm not sure what's the point of that, however that can be replaced by :

md5Password := md5.New()
io.WriteString(md5Password, password)
newPass := fmt.Sprintf("%x", md5Password.Sum(nil))

The original book might be outdated, but there's absolutely no need for that extra bytes.Buffer step.

huangapple
  • 本文由 发表于 2014年8月15日 05:53:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/25318164.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定