This commit is contained in:
Steve Faulkner 2018-11-16 22:13:55 -05:00
Родитель c57b6cb71d
Коммит ca2095d19a
7 изменённых файлов: 142 добавлений и 0 удалений

24
src/browser.test.ts Normal file
Просмотреть файл

@ -0,0 +1,24 @@
import { sign } from "./browser";
const date = new Date("Sat, 17 Nov 2018 01:06:05 GMT");
const masterKey =
"BGejtJoCPl06h6oxaEVI1qXENjaBFNJn47GTfJR5V8bq00v3GiOn36ZZgQv9ogDJfvxEAi4RJi0nEscmXGAe5w==";
const method = "GET";
const resourceId = "";
const resourceType = "";
const expected = encodeURIComponent(
"type=master&ver=1.0&sig=dFkNJGBUXu+ggUJnH1qh+7S1K7BcFdYYtxggMonBH8I="
);
const result = sign(masterKey, method, resourceType, resourceId, date);
if (expected !== result) {
console.log("FAILURE!");
console.log("Expected:", expected);
console.log("Result:", result);
process.exit(1);
}
console.log("BROWSER PASSED");
process.exit(0);

10
src/browser.ts Normal file
Просмотреть файл

@ -0,0 +1,10 @@
import { buildSign } from "./buildSign";
import sha256 from "crypto-js/hmac-sha256";
import Base64 from "crypto-js/enc-base64";
const hmac = (key: string, message: string) => {
var hash = sha256(message, Base64.parse(key));
return Base64.stringify(hash);
};
export const sign = buildSign(hmac);

48
src/buildSign.ts Normal file
Просмотреть файл

@ -0,0 +1,48 @@
const type = "master";
const version = "1.0";
type ResourceType =
| "dbs"
| "colls"
| "docs"
| "sprocs"
| "udfs"
| "triggers"
| "users"
| "permissions"
| "attachments"
| "media"
| "conflicts"
| "pkranges"
| "offers"
| "";
type Method = "GET" | "POST" | "PUT" | "PATCH" | "DELETE";
export function buildSign(hmac: (key: string, message: string) => string) {
return function(
masterKey: string,
method: Method,
resourceType: ResourceType = "",
resourceId: string = "",
date: Date = new Date()
) {
var text =
method.toLowerCase() +
"\n" +
resourceType.toLowerCase() +
"\n" +
resourceId +
"\n" +
date.toUTCString().toLowerCase() +
"\n" +
"" +
"\n";
var signature = hmac(masterKey, text);
return encodeURIComponent(
"type=" + type + "&ver=" + version + "&sig=" + signature
);
};
}

24
src/node.test.ts Normal file
Просмотреть файл

@ -0,0 +1,24 @@
import { sign } from "./node";
const date = new Date("Sat, 17 Nov 2018 01:06:05 GMT");
const masterKey =
"BGejtJoCPl06h6oxaEVI1qXENjaBFNJn47GTfJR5V8bq00v3GiOn36ZZgQv9ogDJfvxEAi4RJi0nEscmXGAe5w==";
const method = "GET";
const resourceId = "";
const resourceType = "";
const expected = encodeURIComponent(
"type=master&ver=1.0&sig=dFkNJGBUXu+ggUJnH1qh+7S1K7BcFdYYtxggMonBH8I="
);
const result = sign(masterKey, method, resourceType, resourceId, date);
if (expected !== result) {
console.log("FAILURE!");
console.log("Expected:", expected);
console.log("Result:", result);
process.exit(1);
}
console.log("NODE PASSED");
process.exit(0);

10
src/node.ts Normal file
Просмотреть файл

@ -0,0 +1,10 @@
import { createHmac } from "crypto";
import { buildSign } from "./buildSign";
const hmac = (key: string, message: string) => {
return createHmac("sha256", Buffer.from(key, "base64"))
.update(message)
.digest("base64");
};
export const sign = buildSign(hmac);

16
src/test.js Normal file
Просмотреть файл

@ -0,0 +1,16 @@
const crypto = require("crypto");
const hmac = require("crypto-js/hmac-sha256");
const Base64 = require("crypto-js/enc-base64");
const key = "test";
const message = "foo";
const sig = hmac(message, key);
console.log("hmac", Base64.stringify(sig));
const signature = crypto
.createHmac("sha256", key)
.update(message)
.digest("base64");
console.log("create-hmac", signature);

10
webpack.config.js Normal file
Просмотреть файл

@ -0,0 +1,10 @@
var path = require("path");
var webpack = require("webpack");
module.exports = (env, argv) => ({
entry: "./lib/browser.js",
output: {
filename: "bundle.js",
path: path.resolve(__dirname)
}
});