{"id":21102159,"url":"https://github.com/juanelas/object-sha","last_synced_at":"2025-10-30T21:45:16.338Z","repository":{"id":40843330,"uuid":"249832551","full_name":"juanelas/object-sha","owner":"juanelas","description":"A package to perform SHA hash functions over key-value objects that works both in Node.js and native JS. It also can be used with typescript projects.  It provides a single method `digest()` that first creates an array of the object values ordered by the object keys (order in JS objects is not guaranteed); then, it JSON.stringify it; and finally it hashes it. The output is a string with the hexadecimal representation of the digest.  Internally the hash is computed using node crypto (node.js) or subtle crypto (browsers). In order to guarantee native compatibility with browsers, the supported hash algorithms are SHA-1, SHA-256, SHA-384 and SHA-512.","archived":false,"fork":false,"pushed_at":"2023-04-14T12:38:42.000Z","size":360,"stargazers_count":3,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-10-29T00:14:18.306Z","etag":null,"topics":["hash","hashable","object","sha2"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/juanelas.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","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":"2020-03-24T22:32:17.000Z","updated_at":"2023-03-06T16:32:58.000Z","dependencies_parsed_at":"2024-06-19T06:11:43.564Z","dependency_job_id":"a6a19ec6-6174-4396-a97f-6ff96aaf14b7","html_url":"https://github.com/juanelas/object-sha","commit_stats":{"total_commits":37,"total_committers":2,"mean_commits":18.5,"dds":"0.027027027027026973","last_synced_commit":"bad01ed37b51a04b443c900a2e875ef58bfbf2b5"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juanelas%2Fobject-sha","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juanelas%2Fobject-sha/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juanelas%2Fobject-sha/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/juanelas%2Fobject-sha/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/juanelas","download_url":"https://codeload.github.com/juanelas/object-sha/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":225444688,"owners_count":17475354,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["hash","hashable","object","sha2"],"created_at":"2024-11-19T23:54:16.973Z","updated_at":"2025-10-30T21:45:16.269Z","avatar_url":"https://github.com/juanelas.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](https://opensource.org/licenses/MIT)\n[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-2.1-4baaaa.svg)](CODE_OF_CONDUCT.md)\n[![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)\n[![Node.js CI](https://github.com/juanelas/object-sha/actions/workflows/build-and-test.yml/badge.svg)](https://github.com/juanelas/object-sha/actions/workflows/build-and-test.yml)\n[![Coverage Status](https://coveralls.io/repos/github/juanelas/object-sha/badge.svg?branch=main)](https://coveralls.io/github/juanelas/object-sha?branch=main)\n\n# object-sha\n\nA package to compute the SHA hash of a JS Object. It works in Node.js and native JS and can be directly imported into TypeScript projects (types provided).\n\nIt includes two methods `hashable(obj)` and `digest(obj)` (see [API Ref Doc](#api-reference-documentation) below):\n\n- `hashable(obj)` prepares any object with no known order to be hashable. It recursively traverses the input to find `Object`s. Then every `Object` is converted to an array sorted by key of 2-arrays [key, value]. The final result is JSON.stringify-ed and returned as a string. Since a specific order is now guaranteed, we can safely use the output as the input for any hash algorithm.\n\n- `digest(obj, [algorithm])` performs an SHA-2 hash to the input obj, which is first made hashable with `hashable(obj)`. The output is a string with the hexadecimal representation of the digest. Internally the hash is computed using node crypto (node.js) or subtle crypto (browsers). Supported hash algorithms are `SHA-1`, `SHA-256`, `SHA-384` and `SHA-512`.\n\n## Usage\n\n`object-sha` can be imported to your project with `npm`:\n\n```console\nnpm install object-sha\n```\n\nThen either require (Node.js CJS):\n\n```javascript\nconst objectSha = require('object-sha')\n```\n\nor import (JavaScript ES module):\n\n```javascript\nimport * as objectSha from 'object-sha'\n```\n\nThe appropriate version for browser or node is automatically exported.\n\nYou can also download the [IIFE bundle](https://raw.githubusercontent.com/juanelas/object-sha/main/dist/bundle.iife.js), the [ESM bundle](https://raw.githubusercontent.com/juanelas/object-sha/main/dist/bundle.esm.min.js) or the [UMD bundle](https://raw.githubusercontent.com/juanelas/object-sha/main/dist/bundle.umd.js) and manually add it to your project, or, if you have already installed `object-sha` in your project, just get the bundles from `node_modules/object-sha/dist/bundles/`.\n\nAn example of usage could be:\n\n```typescript\nconst objectSha = require('object-sha') // or import * as objectSha from 'object-sha'\n\nconst obj1 = { src: 'A', dst: 'B', msg: { hello: 'goodbye!', arr: [2, 9, { b: 5, a: 7 }] } }\nconst obj2 = { dst: 'B', src: 'A', msg: { arr: [2, 9, { a: 7, b: 5 }], hello: 'goodbye!' } }\n\nconsole.log(objectSha.hashable(obj1)) // [[\"dst\",\"B\"],[\"msg\",[[\"arr\",[2,9,[[\"a\",7],[\"b\",5]]]],[\"hello\",\"goodbye!\"]]],[\"src\",\"A\"]]\nconsole.log(objectSha.hashable(obj2)) // [[\"dst\",\"B\"],[\"msg\",[[\"arr\",[2,9,[[\"a\",7],[\"b\",5]]]],[\"hello\",\"goodbye!\"]]],[\"src\",\"A\"]]\n\nobjectSha.digest(obj1).then(console.log) // 6269af73d25f886a50879942cdf5c40500371c6f4d510cec0a67b2992b0a9549\nobjectSha.digest(obj2).then(console.log) // 6269af73d25f886a50879942cdf5c40500371c6f4d510cec0a67b2992b0a9549\n\nobjectSha.digest(obj1, 'SHA-512').then(console.log) // f3325ec4c42cc0154c6a9c78446ce3915196c6ae62d077838b699ca83faa2bd2c0639dd6ca43561afb28bfeb2ffd7481b45c07eaebb7098e1c62ef3c0d441b0b\nobjectSha.digest(obj2, 'SHA-512').then(console.log) // f3325ec4c42cc0154c6a9c78446ce3915196c6ae62d077838b699ca83faa2bd2c0639dd6ca43561afb28bfeb2ffd7481b45c07eaebb7098e1c62ef3c0d441b0b\n\n```\n\n## API reference documentation\n\n[Check the API](./docs/API.md)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuanelas%2Fobject-sha","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjuanelas%2Fobject-sha","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjuanelas%2Fobject-sha/lists"}