@allmaps/id
Uses the SHA-512 algorithm to generate IDs from strings, random IDs from UUIDs and checksums from JSON objects.
Allmaps uses this module to create IDs from IIIF URIs. For example:
Usage
This is an ESM-only module that works in browsers or in Node.js.
Node.js:
The Node.js version of @allmaps/id usescrypto.createHash()
and crypto.randomUUID()
from the crypto
module. First, run npm install @allmaps/id
.
import { generateId } from '@allmaps/id'
const url =
'https://orka.bibliothek.uni-kassel.de/viewer/rest/iiif/manifests/1535113582549/manifest/'
const id = await generateId(url)
console.log(id)
Browser:
The browser version of @allmaps/id uses the SubtleCrypto.digest()
and Crypto.randomUUID()
Web APIs.
<script type="module">
import { generateId } from 'https://unpkg.com/@allmaps/id?module'
const url =
'https://orka.bibliothek.uni-kassel.de/viewer/rest/iiif/manifests/1535113582549/manifest/'
const id = await generateId(url)
console.log(id)
</script>
API
Table of Contents
generateId
Generates a ID from a string using the SHA-512 algorithm. Given the same input, the ID will always be the same.
Parameters
str
string Input stringlength
number Length of returned hash (optional, default 16
)
Returns any First length
characters of the SHA-512 hash of str
generateRandomId
Generates a random ID.
Parameters
length
number Length of returned hash (optional, default 16
)
Returns string First length
characters of the SHA-512 hash of a random UUID.
generateChecksum
Generates a checksum of a JSON object.
Parameters
obj
Object JSON objectlength
number Length of returned hash (optional, default 16
)
Returns string First length
characters of the SHA-512 hash of sorted and serialized version of obj
.