Add map import and export with durable refresh progress
This commit is contained in:
23
lib/map-archive-signature.ts
Normal file
23
lib/map-archive-signature.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import { createHmac, timingSafeEqual } from "node:crypto";
|
||||
import {
|
||||
archiveMetadataSchema,
|
||||
type ArchiveMetadata,
|
||||
} from "../shared/map-archive";
|
||||
|
||||
export function archiveSignature(value: ArchiveMetadata, key: string) {
|
||||
const { signature: _signature, ...metadata } =
|
||||
archiveMetadataSchema.strip().parse(value);
|
||||
return createHmac("sha256", key)
|
||||
.update("minegeo-map-share/v1\n")
|
||||
.update(JSON.stringify(metadata))
|
||||
.digest("hex");
|
||||
}
|
||||
export function verifyArchiveSignature(value: ArchiveMetadata, key: string) {
|
||||
const expected = Buffer.from(archiveSignature(value, key), "hex");
|
||||
const supplied = Buffer.from(value.signature, "hex");
|
||||
if (
|
||||
expected.length !== supplied.length ||
|
||||
!timingSafeEqual(expected, supplied)
|
||||
)
|
||||
throw new Error("Map ZIP ownership verification failed");
|
||||
}
|
||||
Reference in New Issue
Block a user