FAQ
I'm writing a unit test to ensure that my hashing function works correctly.
It takes a string, converts it to a byte array, runs an sha256 hash on it,
then converts the hash to a base64 encoded string.

There seems to be some sort of problem in that the value I get out is not
the same value I get from making the same calculation using external tools.
The value is consistent at least and appears to be cascading when I change
the input, but it's small comfort because it doesn't match the
independently calculated values at all.

Here is some code, hopefully someone can let me know if I'm doing something
wrong, or maybe just calculating the expected output incorrectly...

given

func HashIt(in string) string {
     hasher := sha256.New()
     hasher.Write([]byte(in))
     out := base64.URLEncoding.EncodeToString(hasher.Sum(nil))
     return out
}


With test case...

func main() {
     in := "The quick brown fox, jumped over the lazy dog!"
     out := hashing.HashIt(in)
     fmt.Printf("in: %+v \t out: %+v\n", in, out)
}


I'm getting...

in: The quick brown fox, jumped over the lazy dog! out:
GJ31mY7s2N86FJJ4iddQr0GPUK7e9xDDrBHVunZHLrk=

But I'm expecting out to be ...
MTg5ZGY1OTk4ZWVjZDhkZjNhMTQ5Mjc4ODlkNzUwYWY0MThmNTBhZWRlZjcxMGMzYWMxMWQ1YmE3NjQ3MmViOQ
==

The expected result was calculated by using the SHA256 tool at
http://www.xorbin.com/tools/sha256-hash-calculator and then pasting the
output into it's corresponding Base64 encoder at
http://www.xorbin.com/tools/base64-encoder-and-decoder

Thanks for your help/advice.

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Search Discussions

  • Egon at Jan 20, 2015 at 1:36 pm

    On Tuesday, 20 January 2015 14:23:16 UTC+2, maxpow...@gmail.com wrote:
    I'm writing a unit test to ensure that my hashing function works correctly.
    It takes a string, converts it to a byte array, runs an sha256 hash on it,
    then converts the hash to a base64 encoded string.

    There seems to be some sort of problem in that the value I get out is not
    the same value I get from making the same calculation using external tools.
    The value is consistent at least and appears to be cascading when I change
    the input, but it's small comfort because it doesn't match the
    independently calculated values at all.

    Here is some code, hopefully someone can let me know if I'm doing
    something wrong, or maybe just calculating the expected output
    incorrectly...

    given

    func HashIt(in string) string {
    hasher := sha256.New()
    hasher.Write([]byte(in))
    out := base64.URLEncoding.EncodeToString(hasher.Sum(nil))
    return out
    }


    With test case...

    func main() {
    in := "The quick brown fox, jumped over the lazy dog!"
    out := hashing.HashIt(in)
    fmt.Printf("in: %+v \t out: %+v\n", in, out)
    }


    I'm getting...

    in: The quick brown fox, jumped over the lazy dog! out:
    GJ31mY7s2N86FJJ4iddQr0GPUK7e9xDDrBHVunZHLrk=

    But I'm expecting out to be ...

    MTg5ZGY1OTk4ZWVjZDhkZjNhMTQ5Mjc4ODlkNzUwYWY0MThmNTBhZWRlZjcxMGMzYWMxMWQ1YmE3NjQ3MmViOQ
    ==

    The expected result was calculated by using the SHA256 tool at
    http://www.xorbin.com/tools/sha256-hash-calculator and then pasting the
    output into it's corresponding Base64 encoder at
    http://www.xorbin.com/tools/base64-encoder-and-decoder

    Thanks for your help/advice.
    The result from Go is the "correct"
    answer: http://play.golang.org/p/rmpLhqOiWX.

    When using the http://www.xorbin.com/tools/base64-encoder-and-decoder you
    were hashing the hex representation of the hash, not the hash itself.

    PS. use play.golang.com to show examples, they are easier to work with.

    + Egon



    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Sean Russell at Jan 20, 2015 at 1:37 pm
    Hi,

    On Tuesday, January 20, 2015 at 7:23:16 AM UTC-5, maxpow...@gmail.com wrote:
    ...
    The value is consistent at least and appears to be cascading when I change
    the input, but it's small comfort because it doesn't match the
    independently calculated values at all.
    ...

    The problem isn't in the SHA256 calls, but in the base64 encoding. The hash
    is identical. The difference in Base64 output is that in the Go code,
    you're Base64-encoding the *byte values*, and in your tools, your
    Base64-encoding the *hexadecimal string representation* of the byte values.

    Here's an explanation in code <https://play.golang.org/p/Xg-7bZ90K2>.

    --- SER

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Sean Russell at Jan 20, 2015 at 1:39 pm
    Christ. "you're," not "your", in the last "your."

    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Jim Cote at Jan 20, 2015 at 1:46 pm
    The HashIt function is working fine. The problem is how you are using the
    xorbin tools. You are encoding the hexadecimal representation of the hash
      rather than the bytes.

    On Tuesday, January 20, 2015 at 7:23:16 AM UTC-5, maxpow...@gmail.com wrote:

    I'm writing a unit test to ensure that my hashing function works correctly.
    It takes a string, converts it to a byte array, runs an sha256 hash on it,
    then converts the hash to a base64 encoded string.

    There seems to be some sort of problem in that the value I get out is not
    the same value I get from making the same calculation using external tools.
    The value is consistent at least and appears to be cascading when I change
    the input, but it's small comfort because it doesn't match the
    independently calculated values at all.

    Here is some code, hopefully someone can let me know if I'm doing
    something wrong, or maybe just calculating the expected output
    incorrectly...

    given

    func HashIt(in string) string {
    hasher := sha256.New()
    hasher.Write([]byte(in))
    out := base64.URLEncoding.EncodeToString(hasher.Sum(nil))
    return out
    }


    With test case...

    func main() {
    in := "The quick brown fox, jumped over the lazy dog!"
    out := hashing.HashIt(in)
    fmt.Printf("in: %+v \t out: %+v\n", in, out)
    }


    I'm getting...

    in: The quick brown fox, jumped over the lazy dog! out:
    GJ31mY7s2N86FJJ4iddQr0GPUK7e9xDDrBHVunZHLrk=

    But I'm expecting out to be ...

    MTg5ZGY1OTk4ZWVjZDhkZjNhMTQ5Mjc4ODlkNzUwYWY0MThmNTBhZWRlZjcxMGMzYWMxMWQ1YmE3NjQ3MmViOQ
    ==

    The expected result was calculated by using the SHA256 tool at
    http://www.xorbin.com/tools/sha256-hash-calculator and then pasting the
    output into it's corresponding Base64 encoder at
    http://www.xorbin.com/tools/base64-encoder-and-decoder

    Thanks for your help/advice.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.
  • Maxpowers477 at Jan 20, 2015 at 3:15 pm
    Awesome answers everyone, thank you.
    I figured it might be Pebcak related, but I needed the confirmation since
    this bit is going to be in some very important places.
    Also in the future I'll post code to the playground.
    On Tuesday, January 20, 2015 at 4:23:16 AM UTC-8, maxpow...@gmail.com wrote:

    I'm writing a unit test to ensure that my hashing function works correctly.
    It takes a string, converts it to a byte array, runs an sha256 hash on it,
    then converts the hash to a base64 encoded string.

    There seems to be some sort of problem in that the value I get out is not
    the same value I get from making the same calculation using external tools.
    The value is consistent at least and appears to be cascading when I change
    the input, but it's small comfort because it doesn't match the
    independently calculated values at all.

    Here is some code, hopefully someone can let me know if I'm doing
    something wrong, or maybe just calculating the expected output
    incorrectly...

    given

    func HashIt(in string) string {
    hasher := sha256.New()
    hasher.Write([]byte(in))
    out := base64.URLEncoding.EncodeToString(hasher.Sum(nil))
    return out
    }


    With test case...

    func main() {
    in := "The quick brown fox, jumped over the lazy dog!"
    out := hashing.HashIt(in)
    fmt.Printf("in: %+v \t out: %+v\n", in, out)
    }


    I'm getting...

    in: The quick brown fox, jumped over the lazy dog! out:
    GJ31mY7s2N86FJJ4iddQr0GPUK7e9xDDrBHVunZHLrk=

    But I'm expecting out to be ...

    MTg5ZGY1OTk4ZWVjZDhkZjNhMTQ5Mjc4ODlkNzUwYWY0MThmNTBhZWRlZjcxMGMzYWMxMWQ1YmE3NjQ3MmViOQ
    ==

    The expected result was calculated by using the SHA256 tool at
    http://www.xorbin.com/tools/sha256-hash-calculator and then pasting the
    output into it's corresponding Base64 encoder at
    http://www.xorbin.com/tools/base64-encoder-and-decoder

    Thanks for your help/advice.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/d/optout.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedJan 20, '15 at 12:23p
activeJan 20, '15 at 3:15p
posts6
users4
websitegolang.org

People

Translate

site design / logo © 2022 Grokbase