cloudkit-js
Version:
A JS library for managing a CloudKit container
44 lines (43 loc) • 1.48 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.SignService = void 0;
const crypto_1 = __importDefault(require("crypto"));
class SignService {
constructor(privateKey, keyId) {
this.privateKey = privateKey;
this.keyId = keyId;
}
getDateString() {
// We need to strip out the milliseconds
return new Date().toISOString().replace(/\.[0-9]+?Z/, "Z");
}
signRequest(payload, requestPath) {
const hash = crypto_1.default.createHash("sha256");
const sign = crypto_1.default.createSign("RSA-SHA256");
const now = this.getDateString();
// Hash the payload
hash.update(payload, "utf8");
// Base64 it
const base64PayloadHash = hash.digest("base64");
// Now create a signature.
// It's comprised of [dateString]:[base64PayloadHash]:[requestPath]
const signatureData = [
now,
base64PayloadHash,
requestPath
].join(":");
// Now we'll RSA-SHA256 sign the signatureData with our private key
sign.update(signatureData);
return {
signature: sign.sign(this.privateKey, "base64"),
dateString: now
};
}
getKeyId() {
return this.keyId;
}
}
exports.SignService = SignService;