You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Add
// #import <CommonCrypto/CommonHMAC.h>
// to the bridging Objective-C bridging header.
import Foundation
enum CryptoAlgorithm {
case MD5, SHA1, SHA224, SHA256, SHA384, SHA512
var HMACAlgorithm: CCHmacAlgorithm {
var result: Int = 0
switch self {
case .MD5: result = kCCHmacAlgMD5
case .SHA1: result = kCCHmacAlgSHA1
case .SHA224: result = kCCHmacAlgSHA224
case .SHA256: result = kCCHmacAlgSHA256
case .SHA384: result = kCCHmacAlgSHA384
case .SHA512: result = kCCHmacAlgSHA512
}
return CCHmacAlgorithm(result)
}
var digestLength: Int {
var result: Int32 = 0
switch self {
case .MD5: result = CC_MD5_DIGEST_LENGTH
case .SHA1: result = CC_SHA1_DIGEST_LENGTH
case .SHA224: result = CC_SHA224_DIGEST_LENGTH
case .SHA256: result = CC_SHA256_DIGEST_LENGTH
case .SHA384: result = CC_SHA384_DIGEST_LENGTH
case .SHA512: result = CC_SHA512_DIGEST_LENGTH
}
return Int(result)
}
}
extension String {
func hmac(algorithm: CryptoAlgorithm, key: String) -> String {
let str = self.cStringUsingEncoding(NSUTF8StringEncoding)
let strLen = Int(self.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
let digestLen = algorithm.digestLength
let result = UnsafeMutablePointer<CUnsignedChar>.alloc(digestLen)
let keyStr = key.cStringUsingEncoding(NSUTF8StringEncoding)
let keyLen = Int(key.lengthOfBytesUsingEncoding(NSUTF8StringEncoding))
CCHmac(algorithm.HMACAlgorithm, keyStr!, keyLen, str!, strLen, result)
let digest = stringFromResult(result, length: digestLen)
result.dealloc(digestLen)
return digest
}
private func stringFromResult(result: UnsafeMutablePointer<CUnsignedChar>, length: Int) -> String {
var hash = NSMutableString()
for i in 0..<length {
hash.appendFormat("%02x", result[i])
}
return String(hash)
}
}
print("Message".hmac(CryptoAlgorithm.SHA256, key: "secret")) //aa747c502a898200f9e4fa21bac68136f886a0e27aec70ba06daf2e2a5cb5597
lethex_of_bytesb=let s =Buffer.create (Bytes.length b *2) inBytes.iter (func -> Printf.bprintf s "%02x" (Char.code c)) b;
Buffer.contents s
lethmac_sha256~keypayload=letopenSodium.Auth.Hmac_sha256.Bytesinlet state = init (Bytes.of_string key) in
update state (Bytes.of_string payload);
hex_of_bytes (of_auth (Sodium.Auth.Hmac_sha256.final state))
final key = utf8.encode(__YOUR_SECRET_HERE__);
final bytes = utf8.encode(__DATA__);
final hmacSha256 = Hmac(sha256, key);
final digest = hmacSha256.convert(bytes);
varencoding=newSystem.Text.ASCIIEncoding();byte[]keyBytes=encoding.GetBytes("secret");// secret keybyte[]userIdBytes=encoding.GetBytes("Message");// user id or emailusingvarhmacsha256=newSystem.Security.Cryptography.HMACSHA256(keyBytes);byte[]hash=hmacsha256.ComputeHash(userIdBytes);vartoken=BitConverter.ToString(hash).Replace("-",string.Empty).ToLowerInvariant();Console.WriteLine(token);// aa747c502a898200f9e4fa21bac68136f886a0e27aec70ba06daf2e2a5cb5597
Note: this will run exactly as-is in Program.cs of a bare-bones .NET 7 console project.
import'dart:convert';
import'dart:io';
import'package:crypto/crypto.dart';
StringgenerateIntercomIdentityVerification(String userIdOrEmail) {
// Create an HMAC instance with the SHA-256 algorithm and the secret keyfinal hmac =Hmac(
sha256,
utf8.encode('YOUR_SECRET'),
);
// Generate the HMAC digest of the datafinal digest = hmac.convert(utf8.encode(userIdOrEmail));
// Convert the digest to a string representationreturn digest.toString();
}
Update for Swift 5