1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.getSignedCodeVersion = getSignedCodeVersion;
|
7 | exports.signCode = signCode;
|
8 | exports.signCodeStream = signCodeStream;
|
9 | exports.verifySignedCode = verifySignedCode;
|
10 |
|
11 | var _md = _interopRequireDefault(require("md5"));
|
12 |
|
13 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
14 |
|
15 | const VERSION_COMMENT_RE = /\/\/ flow-typed version: (.*)$/;
|
16 |
|
17 | function getSignedCodeVersion(signedCode) {
|
18 | const [_, versionComment] = signedCode.split('\n');
|
19 | const versionMatches = versionComment.trim().match(VERSION_COMMENT_RE);
|
20 |
|
21 | if (versionMatches == null) {
|
22 | return null;
|
23 | }
|
24 |
|
25 | return versionMatches[1];
|
26 | }
|
27 |
|
28 | function signCode(code, version) {
|
29 | const versionedCode = `// flow-typed version: ${version}\n\n${code}`;
|
30 | const hash = (0, _md.default)(versionedCode);
|
31 | return `// flow-typed signature: ${hash}\n${versionedCode}`;
|
32 | }
|
33 |
|
34 | function signCodeStream(version) {
|
35 | return code => signCode(code, version);
|
36 | }
|
37 |
|
38 | const HASH_COMMENT_RE = /\/\/ flow-typed signature: (.*)$/;
|
39 |
|
40 | function verifySignedCode(signedCode) {
|
41 | const signedCodeLines = signedCode.split('\n');
|
42 | const [hashComment] = signedCodeLines;
|
43 | const hashMatches = hashComment.trim().match(HASH_COMMENT_RE);
|
44 |
|
45 | if (hashMatches == null) {
|
46 | return false;
|
47 | }
|
48 |
|
49 | const [_, hash] = hashMatches;
|
50 | const versionedCode = signedCodeLines.slice(1).join('\n');
|
51 | return (0, _md.default)(versionedCode) === hash;
|
52 | } |
\ | No newline at end of file |