|
| 1 | +import { DigestSource } from './utils.js'; |
| 2 | +import type { DigestHashAlgorithm, IncomingRequest } from '../types.js'; |
| 3 | +import * as sh from 'structured-headers'; |
| 4 | +export declare class RFC9530GenerateDigestHeaderError extends Error { |
| 5 | + constructor(message: string); |
| 6 | +} |
| 7 | +export type RFC9530HashAlgorithmStatus = 'Active' | 'Provisional' | 'Deprecated'; |
| 8 | +export declare const RFC9530HashAlgorithmRegistry: { |
| 9 | + 'sha-512': "Active"; |
| 10 | + 'sha-256': "Active"; |
| 11 | + md5: "Deprecated"; |
| 12 | + sha: "Deprecated"; |
| 13 | + unixsum: "Deprecated"; |
| 14 | + unixcksum: "Deprecated"; |
| 15 | + adler: "Deprecated"; |
| 16 | + crc32c: "Deprecated"; |
| 17 | +}; |
| 18 | +export type RFC9530HashAlgorithm = keyof typeof RFC9530HashAlgorithmRegistry; |
| 19 | +export declare const supportedHashAlgorithmsWithRFC9530AndWebCrypto: ("sha-512" | "sha-256")[]; |
| 20 | +/** |
| 21 | + * Want-*-Digest parsed by structured-headers.parseDictionary |
| 22 | + * https://datatracker.ietf.org/doc/html/rfc9530#name-integrity-preference-fields |
| 23 | + */ |
| 24 | +export type RFC9530Prefernece = Map<string, [number, Map<any, any>]>; |
| 25 | +export declare function convertHashAlgorithmFromWebCryptoToRFC9530(algo: DigestHashAlgorithm): RFC9530HashAlgorithm; |
| 26 | +/** |
| 27 | + * @param prefernece Prefernece map (Want-*-Digest field parsed by structured-headers.parseDictionary) |
| 28 | + * @param meAcceptable The hash algorithms that You can accept or use |
| 29 | + * @returns |
| 30 | + */ |
| 31 | +export declare function chooseRFC9530HashAlgorithmByPreference(prefernece: RFC9530Prefernece, meAcceptable?: RFC9530HashAlgorithm[]): RFC9530HashAlgorithm | null; |
| 32 | +export type RFC9530DigestHeaderObject = [string, [sh.ByteSequence, Map<any, any>]][]; |
| 33 | +/** |
| 34 | + * Generate single Digest header |
| 35 | + * @param body The body to be hashed |
| 36 | + * @param hashAlgorithm |
| 37 | + * Supported common to RFC 9530 Registered and SubtleCrypto.digest = Only 'SHA-256' and 'SHA-512' |
| 38 | + * @returns `[[algorithm, [ByteSequence, Map(0)]]]` |
| 39 | + * To convert to string, use serializeDictionary from structured-headers |
| 40 | + */ |
| 41 | +export declare function genSingleRFC9530DigestHeader(body: DigestSource, hashAlgorithm: string): Promise<RFC9530DigestHeaderObject>; |
| 42 | +/** |
| 43 | + * Generate Digest header |
| 44 | + * @param body The body to be hashed |
| 45 | + * @param hashAlgorithms |
| 46 | + * Supported common to RFC 9530 Registered and SubtleCrypto.digest = Only 'SHA-256' and 'SHA-512' |
| 47 | + * @param process |
| 48 | + * 'concurrent' to use Promise.all, 'sequential' to use for..of |
| 49 | + * @default 'concurrent' |
| 50 | + * @returns `[algorithm, [ByteSequence, Map(0)]][]` |
| 51 | + * To convert to string, use serializeDictionary from structured-headers |
| 52 | + */ |
| 53 | +export declare function genRFC9530DigestHeader(body: DigestSource, hashAlgorithms?: string | RFC9530Prefernece | Iterable<string>, process?: 'concurrent' | 'sequential'): Promise<RFC9530DigestHeaderObject>; |
| 54 | +/** |
| 55 | + * Verify Content-Digest header (not Repr-Digest) |
| 56 | + * @param request IncomingRequest |
| 57 | + * @param rawBody Raw body |
| 58 | + * @param opts Options |
| 59 | + * @param errorLogger Error logger when verification fails |
| 60 | + * @returns Whether digest is valid with the body |
| 61 | + */ |
| 62 | +export declare function verifyRFC9530DigestHeader(request: IncomingRequest, rawBody: DigestSource, opts?: { |
| 63 | + /** |
| 64 | + * If false, return true when no Digest header is found |
| 65 | + * @default true |
| 66 | + */ |
| 67 | + failOnNoDigest?: boolean; |
| 68 | + /** |
| 69 | + * If true, verify all digests without not supported algorithms |
| 70 | + * If false, use the first supported and exisiting algorithm in hashAlgorithms |
| 71 | + * @default true |
| 72 | + */ |
| 73 | + verifyAll?: boolean; |
| 74 | + /** |
| 75 | + * Specify hash algorithms you accept. (RFC 9530 algorithm registries) |
| 76 | + * |
| 77 | + * If `varifyAll: false`, it is also used to choose the hash algorithm to verify. |
| 78 | + * (Younger index is preferred.) |
| 79 | + */ |
| 80 | + hashAlgorithms?: RFC9530HashAlgorithm[]; |
| 81 | +}, errorLogger?: ((message: any) => any)): Promise<boolean>; |
0 commit comments