{"id":21971453,"url":"https://github.com/danharper/hmac-examples","last_synced_at":"2026-01-27T06:01:08.906Z","repository":{"id":139307383,"uuid":"65491099","full_name":"danharper/hmac-examples","owner":"danharper","description":"SHA256 HMAC in different languages (both hex \u0026 base64 encoding)","archived":false,"fork":false,"pushed_at":"2024-03-22T15:19:56.000Z","size":16,"stargazers_count":289,"open_issues_count":0,"forks_count":48,"subscribers_count":11,"default_branch":"master","last_synced_at":"2025-10-13T12:05:38.338Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":null,"has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"unlicense","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/danharper.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-08-11T18:09:24.000Z","updated_at":"2025-08-20T13:19:32.000Z","dependencies_parsed_at":null,"dependency_job_id":"4b4bbf98-2704-4f7a-8b9b-78c23d626c24","html_url":"https://github.com/danharper/hmac-examples","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/danharper/hmac-examples","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danharper%2Fhmac-examples","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danharper%2Fhmac-examples/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danharper%2Fhmac-examples/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danharper%2Fhmac-examples/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/danharper","download_url":"https://codeload.github.com/danharper/hmac-examples/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/danharper%2Fhmac-examples/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28805316,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-27T05:43:52.625Z","status":"ssl_error","status_checked_at":"2026-01-27T05:43:48.957Z","response_time":168,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-29T14:50:27.792Z","updated_at":"2026-01-27T06:01:08.800Z","avatar_url":"https://github.com/danharper.png","language":null,"funding_links":[],"categories":["Others"],"sub_categories":[],"readme":"e.g. for webhook hashes\n\n---\n\nExample inputs:\n\n| Variable | Value |\n| --- | --- |\n| key | `the shared secret key here` |\n| message | `the message to hash here ` |\n\nReference outputs for example inputs above:\n\n| Type | Hash |\n| --- | --- |\n| as hexit | `4643978965ffcec6e6d73b36a39ae43ceb15f7ef8131b8307862ebc560e7f988` |\n| as base64 | `RkOXiWX/zsbm1zs2o5rkPOsV9++BMbgweGLrxWDn+Yg=` |\n\n---\n\n## PHP\n\n```php\n\u003c?php\n\n$key = 'the shared secret key here';\n$message = 'the message to hash here';\n\n// to lowercase hexits\nhash_hmac('sha256', $message, $key);\n\n// to base64\nbase64_encode(hash_hmac('sha256', $message, $key, true));\n```\n\n## NodeJS\n\n```js\nvar crypto = require('crypto');\n\nvar key = 'the shared secret key here';\nvar message = 'the message to hash here';\n\nvar hash = crypto.createHmac('sha256', key).update(message);\n\n// to lowercase hexits\nhash.digest('hex');\n\n// to base64\nhash.digest('base64');\n```\n\n## JavaScript ES6\n\n_Using the [Web Crypto API](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API), available in all modern browsers. \u003csup\u003e[[1]](https://caniuse.com/#feat=cryptography)\u003c/sup\u003e_\n\n```js\nconst key = 'the shared secret key here';\nconst message = 'the message to hash here';\n\nconst getUtf8Bytes = str =\u003e\n  new Uint8Array(\n    [...unescape(encodeURIComponent(str))].map(c =\u003e c.charCodeAt(0))\n  );\n\nconst keyBytes = getUtf8Bytes(key);\nconst messageBytes = getUtf8Bytes(message);\n\nconst cryptoKey = await crypto.subtle.importKey(\n  'raw', keyBytes, { name: 'HMAC', hash: 'SHA-256' },\n  true, ['sign']\n);\nconst sig = await crypto.subtle.sign('HMAC', cryptoKey, messageBytes);\n\n// to lowercase hexits\n[...new Uint8Array(sig)].map(b =\u003e b.toString(16).padStart(2, '0')).join('');\n\n// to base64\nbtoa(String.fromCharCode(...new Uint8Array(sig)));\n```\n\n## Ruby\n\n```rb\nrequire 'openssl'\nrequire 'base64'\n\nkey = 'the shared secret key here'\nmessage = 'the message to hash here'\n\n# to lowercase hexits\nOpenSSL::HMAC.hexdigest('sha256', key, message)\n\n# to base64\nBase64.encode64(OpenSSL::HMAC.digest('sha256', key, message))\n```\n\n## Elixir\n\n```elixir\nkey = 'the shared secret key here'\nmessage = 'the message to hash here'\n\nsignature = :crypto.mac(:hmac, :sha256, key, message)\n\n# to lowercase hexits\nBase.encode16(signature, case: :lower)\n\n# to base64\nBase.encode64(signature)\n```\n\n## Go\n\n```go\npackage main\n\nimport (\n\t\"crypto/hmac\"\n\t\"crypto/sha256\"\n\t\"encoding/base64\"\n\t\"encoding/hex\"\n)\n\nfunc main() {\n\tsecret := []byte(\"the shared secret key here\")\n\tmessage := []byte(\"the message to hash here\")\n\t\n\thash := hmac.New(sha256.New, secret)\n\thash.Write(message)\n\t\n\t// to lowercase hexits\n\thex.EncodeToString(hash.Sum(nil))\n\t\n\t// to base64\n\tbase64.StdEncoding.EncodeToString(hash.Sum(nil))\n}\n```\n\n## Python 2\n\n```py\nimport hashlib\nimport hmac\nimport base64\n\nmessage = bytes('the message to hash here').encode('utf-8')\nsecret = bytes('the shared secret key here').encode('utf-8')\n\nhash = hmac.new(secret, message, hashlib.sha256)\n\n# to lowercase hexits\nhash.hexdigest()\n\n# to base64\nbase64.b64encode(hash.digest())\n```\n\n## Python 3\n\n```py\nimport hashlib\nimport hmac\nimport base64\n\nmessage = bytes('the message to hash here', 'utf-8')\nsecret = bytes('the shared secret key here', 'utf-8')\n\nhash = hmac.new(secret, message, hashlib.sha256)\n\n# to lowercase hexits\nhash.hexdigest()\n\n# to base64\nbase64.b64encode(hash.digest())\n```\n\n## C\u0026#35;\n\n```cs\nusing System;\nusing System.Security.Cryptography;\nusing System.Text;\n\nclass MainClass {\n  public static void Main (string[] args) {\n    string key = \"the shared secret key here\";\n    string message = \"the message to hash here\";\n    \n    byte[] keyByte = new ASCIIEncoding().GetBytes(key);\n    byte[] messageBytes = new ASCIIEncoding().GetBytes(message);\n    \n    byte[] hashmessage = new HMACSHA256(keyByte).ComputeHash(messageBytes);\n    \n    // to lowercase hexits\n    String.Concat(Array.ConvertAll(hashmessage, x =\u003e x.ToString(\"x2\")));\n    \n    // to base64\n    Convert.ToBase64String(hashmessage);\n  }\n}\n```\n\n## Java\n\n```java\nimport javax.crypto.Mac;\nimport javax.crypto.spec.SecretKeySpec;\nimport java.security.NoSuchAlgorithmException;\nimport java.security.InvalidKeyException;\nimport javax.xml.bind.DatatypeConverter;\n\nclass Main {\n  public static void main(String[] args) {\n  \ttry {\n\t    String key = \"the shared secret key here\";\n\t    String message = \"the message to hash here\";\n\t    \n\t    Mac hasher = Mac.getInstance(\"HmacSHA256\");\n\t    hasher.init(new SecretKeySpec(key.getBytes(), \"HmacSHA256\"));\n\t    \n\t    byte[] hash = hasher.doFinal(message.getBytes());\n\t    \n\t    // to lowercase hexits\n\t    DatatypeConverter.printHexBinary(hash);\n\t    \n\t    // to base64\n\t    DatatypeConverter.printBase64Binary(hash);\n  \t}\n  \tcatch (NoSuchAlgorithmException e) {}\n  \tcatch (InvalidKeyException e) {}\n  }\n}\n```\n\n## Rust\n\n```rust\nextern crate hmac;\nextern crate sha2;\nextern crate base64;\nextern crate hex;\n\nuse sha2::Sha256;\nuse hmac::{Hmac, Mac};\n\nfn main() {\n    type HmacSha256 = Hmac\u003cSha256\u003e;\n\n    let secret = \"the shared secret key here\";\n    let message = \"the message to hash here\";\n\n    let mut mac = HmacSha256::new_varkey(secret.as_bytes()).unwrap();\n\n    mac.input(message.as_bytes());\n\n    let hash_message = mac.result().code();\n\n    // to lowercase hexits\n    hex::encode(\u0026hash_message);\n\n    // to base64\n    base64::encode(\u0026hash_message);\n}\n```\n\n---\n\n* https://stackoverflow.com/questions/13109588/base64-encoding-in-java\n* http://www.jokecamp.com/blog/examples-of-creating-base64-hashes-using-hmac-sha256-in-different-languages/\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanharper%2Fhmac-examples","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdanharper%2Fhmac-examples","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdanharper%2Fhmac-examples/lists"}