Jelle
Jelle
Explore posts from servers
NNovu
Created by Jelle on 10/13/2023 in #💬│support
Enabling HMAC for in-app with C#
I fixed it with this
28 replies
NNovu
Created by Jelle on 10/13/2023 in #💬│support
Enabling HMAC for in-app with C#
Im pretty sure you just POST this
{
"applicationIdentifier": "<application_identifier>",
"subscriberId": "<subscriberId>",
"hmacHash": "<hash>"
}
{
"applicationIdentifier": "<application_identifier>",
"subscriberId": "<subscriberId>",
"hmacHash": "<hash>"
}
You're not supposed to send your api key over from the client
28 replies
NNovu
Created by Jelle on 10/13/2023 in #💬│support
Enabling HMAC for in-app with C#
Youre not sending the hmac hash though
28 replies
NNovu
Created by Jelle on 10/13/2023 in #💬│support
Enabling HMAC for in-app with C#
And that it's sent correctly to the novu api
28 replies
NNovu
Created by Jelle on 10/13/2023 in #💬│support
Enabling HMAC for in-app with C#
Are you sure you're using the correct values everywhere?
28 replies
NNovu
Created by Jelle on 10/13/2023 in #💬│support
Enabling HMAC for in-app with C#
Yep it does
28 replies
NNovu
Created by Jelle on 10/13/2023 in #💬│support
Enabling HMAC for in-app with C#
Let me check if it still works in our app lmao
28 replies
NNovu
Created by Jelle on 10/13/2023 in #💬│support
Enabling HMAC for in-app with C#
I don't really see anything wrong in your code though, looks the same
28 replies
NNovu
Created by Jelle on 10/13/2023 in #💬│support
Enabling HMAC for in-app with C#
This is my implementation, ComputeHash(string) is called with the subscriberId
/// <summary>
/// Used to generate HMAC encrypted userIds for using the Novu Notification Center API.
/// See: <a href="https://docs.novu.co/notification-center/client/react/get-started#enabling-hmac-encryption">Novu Docs</a> for more info
/// </summary>
public sealed class NovuNotificationCenterHashProvider : INotificationCenterHashProvider, IDisposable
{
private readonly HMACSHA256 _hmac;

/// <param name="key">The secret key for HMACSHA256 encryption. The key can be any length. However, the recommended size is 64 bytes. If the key is more than 64 bytes long, it is hashed (using SHA-256) to derive a 64-byte key. If it is less than 64 bytes long, it is padded to 64 bytes.</param>
/// <exception cref="ArgumentNullException">Thrown if <see cref="key"/> is null or empty</exception>
public NovuNotificationCenterHashProvider(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));

_hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
}

public NovuNotificationCenterHashProvider(IOptions<NovuConfiguration> options) : this(options.Value.ApiKey!)
{
}

public string ComputeHash(string source)
{
var hash = _hmac.ComputeHash(Encoding.UTF8.GetBytes(source));

return ToHex(hash);
}

private static string ToHex(IReadOnlyCollection<byte> hash)
{
var hexBuilder = new StringBuilder(hash.Count * 2);
foreach (var b in hash)
{
// The 'x2' format generates two lowercased hex characters per entry
hexBuilder.Append($"{b:x2}");
}

return hexBuilder.ToString();
}

public void Dispose()
{
_hmac.Dispose();
}
}
/// <summary>
/// Used to generate HMAC encrypted userIds for using the Novu Notification Center API.
/// See: <a href="https://docs.novu.co/notification-center/client/react/get-started#enabling-hmac-encryption">Novu Docs</a> for more info
/// </summary>
public sealed class NovuNotificationCenterHashProvider : INotificationCenterHashProvider, IDisposable
{
private readonly HMACSHA256 _hmac;

/// <param name="key">The secret key for HMACSHA256 encryption. The key can be any length. However, the recommended size is 64 bytes. If the key is more than 64 bytes long, it is hashed (using SHA-256) to derive a 64-byte key. If it is less than 64 bytes long, it is padded to 64 bytes.</param>
/// <exception cref="ArgumentNullException">Thrown if <see cref="key"/> is null or empty</exception>
public NovuNotificationCenterHashProvider(string key)
{
if (string.IsNullOrWhiteSpace(key))
throw new ArgumentNullException(nameof(key));

_hmac = new HMACSHA256(Encoding.UTF8.GetBytes(key));
}

public NovuNotificationCenterHashProvider(IOptions<NovuConfiguration> options) : this(options.Value.ApiKey!)
{
}

public string ComputeHash(string source)
{
var hash = _hmac.ComputeHash(Encoding.UTF8.GetBytes(source));

return ToHex(hash);
}

private static string ToHex(IReadOnlyCollection<byte> hash)
{
var hexBuilder = new StringBuilder(hash.Count * 2);
foreach (var b in hash)
{
// The 'x2' format generates two lowercased hex characters per entry
hexBuilder.Append($"{b:x2}");
}

return hexBuilder.ToString();
}

public void Dispose()
{
_hmac.Dispose();
}
}
28 replies
NNovu
Created by Jelle on 10/13/2023 in #💬│support
Enabling HMAC for in-app with C#
My last message got it working for me
28 replies
NNovu
Created by Jelle on 10/13/2023 in #💬│support
Enabling HMAC for in-app with C#
May be worth adding to the docs that it should be a lowercased hex hash @Pawan Jain
28 replies
NNovu
Created by Jelle on 10/13/2023 in #💬│support
Enabling HMAC for in-app with C#
So I fiddled around with it and turns out that it was happening because the Convert.ToHexString generates it in uppercase, converting it to lowercase fixes it
string ToHex(byte[] hash)
{
var hexBuilder = new StringBuilder(hash.Length * 2);
foreach (var b in hash)
{
// The 'x2' format generates two lowercased hex characters per entry
hexBuilder.Append($"{b:x2}");
}

return hexBuilder.ToString();
}
string ToHex(byte[] hash)
{
var hexBuilder = new StringBuilder(hash.Length * 2);
foreach (var b in hash)
{
// The 'x2' format generates two lowercased hex characters per entry
hexBuilder.Append($"{b:x2}");
}

return hexBuilder.ToString();
}
28 replies
CC#
Created by Jelle on 10/16/2023 in #help
❔ HMAC encrypt a string
Though how would you advise I store the api key? It's now coming from a local file but it'll be fetched from a secret store in test/prod environment I hadn't really thought about text encoding
16 replies
CC#
Created by Jelle on 10/16/2023 in #help
❔ HMAC encrypt a string
In the end I snatched up the code in this post: https://stackoverflow.com/questions/11790599/python-hmac-and-c-sharp-hmac Which in the end just returns the same thing except that it's lowercase instead of uppercase lol
16 replies
CC#
Created by Jelle on 10/16/2023 in #help
❔ HMAC encrypt a string
16 replies
CC#
Created by Jelle on 10/16/2023 in #help
❔ HMAC encrypt a string
Oh man I got it working
16 replies
CC#
Created by Jelle on 10/16/2023 in #help
❔ HMAC encrypt a string
Yep
16 replies
CC#
Created by Jelle on 10/16/2023 in #help
❔ HMAC encrypt a string
I think it's because I'm generating it in UTF-8 and then turning that string into a hex instead of directly to hex?
16 replies
NNovu
Created by Jelle on 10/13/2023 in #💬│support
Enabling HMAC for in-app with C#
The novu api key, app id, and the subscriber id seems to be correct
28 replies
NNovu
Created by Jelle on 10/12/2023 in #💬│support
SES emails not going through
Well anyway thanks guys! 😄
17 replies