FAQ
I am currently working on an library to connect to Azure storage but I have
hit a problem with the golang SHA256 algorithm as the output seems to be
different to C#

The azure C# library uses

internal static string ComputeHmac256(byte[] key, string message)
{
using (HashAlgorithm hashAlgorithm = new HMACSHA256(key))
{
byte[] messageBuffer = Encoding.UTF8.GetBytes(message);
return
Convert.ToBase64String(hashAlgorithm.ComputeHash(messageBuffer));
}
}

I have implemented this using the crypto/hmac & crypto/sha256

c := hmac.New(sha256.New, []byte("{secret-key}"))
sha := c.Sum([]byte({message}))
enc := base64.StdEncoding.EncodeToString(sha)

This problem is that the output from the encoded string seems to be double
the length for the same input in golang. Any ideas what the problem might
be with my implementation?





--
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/groups/opt_out.

Search Discussions

  • Maxim Khitrov at Mar 4, 2013 at 1:50 pm

    On Mon, Mar 4, 2013 at 8:44 AM, Ollie Castle wrote:
    I am currently working on an library to connect to Azure storage but I have
    hit a problem with the golang SHA256 algorithm as the output seems to be
    different to C#

    The azure C# library uses
    internal static string ComputeHmac256(byte[] key, string message)
    {
    using (HashAlgorithm hashAlgorithm = new HMACSHA256(key))
    {
    byte[] messageBuffer = Encoding.UTF8.GetBytes(message);
    return
    Convert.ToBase64String(hashAlgorithm.ComputeHash(messageBuffer));
    }
    }

    I have implemented this using the crypto/hmac & crypto/sha256
    c := hmac.New(sha256.New, []byte("{secret-key}"))
    sha := c.Sum([]byte({message}))
    enc := base64.StdEncoding.EncodeToString(sha)

    This problem is that the output from the encoded string seems to be double
    the length for the same input in golang. Any ideas what the problem might be
    with my implementation?
    c.Sum appends the current hash to the byte slice. You need to call
    c.Write([]byte({message})) followed by c.Sum(nil).

    - Max

    --
    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/groups/opt_out.
  • Oliver Castle at Mar 5, 2013 at 8:16 am
    Thank you for the code. I have implemented the differences but am having
    problems authenticating against Miscrosoft Azure Storage as it seems that
    the SHA256 is being calculated differently in golang. I have hard coded the
    authorization from the .NET code into the golang request and it
    will retrieve the file so I have ruled out problems with the request.

    The input into the SHA256 is the attached file and the hash key is:

    "Vd29s66NwovAvauuXuwMP0A5gDE2M7pG5sDOIV/lydN8wYidcsagY6L6i9nnIssFPIaX+K86b6duKAyazvTExg=="

    GoLang Output: hLrFf/CSFqInqcbcGcKUnj5lAdZYCIiEwEM3pA+ZIZI=
    .NET Output: fZGEHTTCEN6rGsGkXVm9WvmdRTf3gLOSxitynAP2Z30=

    The only problems I can think of is that I am doing something wrong with
    carriage returns or not converting strings to UTF-8.

    The code can be seen here http://play.golang.org/p/H8KlDC0Ogx

    The authentication code is no longer valid against the azure service but I
    can setup a dummy account if any one wants to have a look.

    On Mon, Mar 4, 2013 at 1:50 PM, Maxim Khitrov wrote:

    On Mon, Mar 4, 2013 at 8:44 AM, Ollie Castle
    wrote:
    I am currently working on an library to connect to Azure storage but I have
    hit a problem with the golang SHA256 algorithm as the output seems to be
    different to C#

    The azure C# library uses
    internal static string ComputeHmac256(byte[] key, string
    message)
    {
    using (HashAlgorithm hashAlgorithm = new HMACSHA256(key))
    {
    byte[] messageBuffer = Encoding.UTF8.GetBytes(message);
    return
    Convert.ToBase64String(hashAlgorithm.ComputeHash(messageBuffer));
    }
    }

    I have implemented this using the crypto/hmac & crypto/sha256
    c := hmac.New(sha256.New, []byte("{secret-key}"))
    sha := c.Sum([]byte({message}))
    enc := base64.StdEncoding.EncodeToString(sha)

    This problem is that the output from the encoded string seems to be double
    the length for the same input in golang. Any ideas what the problem might be
    with my implementation?
    c.Sum appends the current hash to the byte slice. You need to call
    c.Write([]byte({message})) followed by c.Sum(nil).

    - Max
    --
    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/groups/opt_out.
  • Maxim Khitrov at Mar 5, 2013 at 12:36 pm
    HMAC-SHA-256 is the same everywhere. The difference is caused either
    by the key or the message (or both). The following code contains a Go
    translation of your .NET ComputeHmac256 function. Play with output and
    key until you figure out where the difference is. HTTP headers are
    separated by \r\n, though I'm not sure why you're building them by
    hand. Does Azure require that specific format?

    http://play.golang.org/p/2SMGny7vWn

    - Max

    On Tue, Mar 5, 2013 at 3:16 AM, Oliver Castle
    wrote:
    Thank you for the code. I have implemented the differences but am having
    problems authenticating against Miscrosoft Azure Storage as it seems that
    the SHA256 is being calculated differently in golang. I have hard coded the
    authorization from the .NET code into the golang request and it will
    retrieve the file so I have ruled out problems with the request.

    The input into the SHA256 is the attached file and the hash key is:

    "Vd29s66NwovAvauuXuwMP0A5gDE2M7pG5sDOIV/lydN8wYidcsagY6L6i9nnIssFPIaX+K86b6duKAyazvTExg=="

    GoLang Output: hLrFf/CSFqInqcbcGcKUnj5lAdZYCIiEwEM3pA+ZIZI=
    .NET Output: fZGEHTTCEN6rGsGkXVm9WvmdRTf3gLOSxitynAP2Z30=

    The only problems I can think of is that I am doing something wrong with
    carriage returns or not converting strings to UTF-8.

    The code can be seen here http://play.golang.org/p/H8KlDC0Ogx

    The authentication code is no longer valid against the azure service but I
    can setup a dummy account if any one wants to have a look.

    On Mon, Mar 4, 2013 at 1:50 PM, Maxim Khitrov wrote:

    On Mon, Mar 4, 2013 at 8:44 AM, Ollie Castle
    wrote:
    I am currently working on an library to connect to Azure storage but I
    have
    hit a problem with the golang SHA256 algorithm as the output seems to be
    different to C#

    The azure C# library uses
    internal static string ComputeHmac256(byte[] key, string
    message)
    {
    using (HashAlgorithm hashAlgorithm = new HMACSHA256(key))
    {
    byte[] messageBuffer = Encoding.UTF8.GetBytes(message);
    return
    Convert.ToBase64String(hashAlgorithm.ComputeHash(messageBuffer));
    }
    }

    I have implemented this using the crypto/hmac & crypto/sha256
    c := hmac.New(sha256.New, []byte("{secret-key}"))
    sha := c.Sum([]byte({message}))
    enc := base64.StdEncoding.EncodeToString(sha)

    This problem is that the output from the encoded string seems to be
    double
    the length for the same input in golang. Any ideas what the problem
    might be
    with my implementation?
    c.Sum appends the current hash to the byte slice. You need to call
    c.Write([]byte({message})) followed by c.Sum(nil).

    - Max
    --
    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/groups/opt_out.
  • Oliver Castle at Mar 5, 2013 at 12:54 pm
    Thank you very much, that solved the issue encoding the key before
    encrypting it.

    Now I can finish off and publish my azure storage api :-)

    On Tue, Mar 5, 2013 at 12:35 PM, Maxim Khitrov wrote:

    HMAC-SHA-256 is the same everywhere. The difference is caused either
    by the key or the message (or both). The following code contains a Go
    translation of your .NET ComputeHmac256 function. Play with output and
    key until you figure out where the difference is. HTTP headers are
    separated by \r\n, though I'm not sure why you're building them by
    hand. Does Azure require that specific format?

    http://play.golang.org/p/2SMGny7vWn

    - Max

    On Tue, Mar 5, 2013 at 3:16 AM, Oliver Castle
    wrote:
    Thank you for the code. I have implemented the differences but am having
    problems authenticating against Miscrosoft Azure Storage as it seems that
    the SHA256 is being calculated differently in golang. I have hard coded the
    authorization from the .NET code into the golang request and it will
    retrieve the file so I have ruled out problems with the request.

    The input into the SHA256 is the attached file and the hash key is:

    "Vd29s66NwovAvauuXuwMP0A5gDE2M7pG5sDOIV/lydN8wYidcsagY6L6i9nnIssFPIaX+K86b6duKAyazvTExg=="
    GoLang Output: hLrFf/CSFqInqcbcGcKUnj5lAdZYCIiEwEM3pA+ZIZI=
    .NET Output: fZGEHTTCEN6rGsGkXVm9WvmdRTf3gLOSxitynAP2Z30=

    The only problems I can think of is that I am doing something wrong with
    carriage returns or not converting strings to UTF-8.

    The code can be seen here http://play.golang.org/p/H8KlDC0Ogx

    The authentication code is no longer valid against the azure service but I
    can setup a dummy account if any one wants to have a look.

    On Mon, Mar 4, 2013 at 1:50 PM, Maxim Khitrov wrote:

    On Mon, Mar 4, 2013 at 8:44 AM, Ollie Castle
    wrote:
    I am currently working on an library to connect to Azure storage but I
    have
    hit a problem with the golang SHA256 algorithm as the output seems to
    be
    different to C#

    The azure C# library uses
    internal static string ComputeHmac256(byte[] key, string
    message)
    {
    using (HashAlgorithm hashAlgorithm = new HMACSHA256(key))
    {
    byte[] messageBuffer =
    Encoding.UTF8.GetBytes(message);
    return
    Convert.ToBase64String(hashAlgorithm.ComputeHash(messageBuffer));
    }
    }

    I have implemented this using the crypto/hmac & crypto/sha256
    c := hmac.New(sha256.New, []byte("{secret-key}"))
    sha := c.Sum([]byte({message}))
    enc := base64.StdEncoding.EncodeToString(sha)

    This problem is that the output from the encoded string seems to be
    double
    the length for the same input in golang. Any ideas what the problem
    might be
    with my implementation?
    c.Sum appends the current hash to the byte slice. You need to call
    c.Write([]byte({message})) followed by c.Sum(nil).

    - Max
    --
    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/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedMar 4, '13 at 1:44p
activeMar 5, '13 at 12:54p
posts5
users2
websitegolang.org

2 users in discussion

Oliver Castle: 3 posts Maxim Khitrov: 2 posts

People

Translate

site design / logo © 2021 Grokbase