Good evening!

I can not figure out how to encrypt Go. I do everything as in the example:

h := sha1.New() io.WriteString(h, "123456") fmt.Println(h.Sum(nil)) 

Everything is OK, at the output I see the promised byte array. But when trying to convert it to a string

 var result = string(h.Sum(nil)) 

It turns out some kind of abracadabra, I just can not understand what's the matter ...

  • Note that SHA, MDx and base64 are not encryption, but hashing. The disadvantage is in the possibility of selecting the original when receiving a hash, if you use something like a password at the entrance. - pirj
  • sha and md5 - hashing. You can search the original by search, although it is difficult. base64 is just encoding (not to be confused with encryption) and its result is directly converted to its original state. - rekby

1 answer 1

For this conversion, I use the following code:

 func foo(someStr string) string { h := md5.New() h.Write([]byte(someStr)) return fmt.Sprintf("%x", h.Sum(nil)) } 

Update . You can still do this:

 func ByteToBase64(value []byte) string { return base64.StdEncoding.EncodeToString(value) } 
  • Cheers-ah-ah, it worked! ) Thank! - Pavel Vershinin