forked from hhvm/hack-codegen
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSignedSource.php
More file actions
74 lines (72 loc) · 2.38 KB
/
SignedSource.php
File metadata and controls
74 lines (72 loc) · 2.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?hh
/**
* Copyright (c) 2015-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*/
/**
* Designate machine-generated code so tools can distinguish it from
* human-generated code, and prevent manual edits of machine-generated code by
* embedding a simple checksum in generated source files.
*
*
* = Generating Signed Source =
*
* When generating source, use SignedSource to sign the file. This will prevent
* it from being checked in if it is manually edited. Signing is a two step
* process:
*
* 1. Call SignedSource::getSigningToken() and embed the return string
* somewhere in your source file (generally, in a header comment).
* 2. After generating the file, call SignedSource::signFile($file).
*
* For example:
*
* $signature_token = SignedSource::getSigningToken();
* $generated_file = <<<EODOC
* /**
* * This file is generated. Do not modify it manually!
* *
* * {$signature_token}
* *
* ...
*
* $signed_file = SignedSource::signFile($generated_file);
* Filesystem::writeFileIfChanged('/path/to/generated/file', $signed_file);
*
*
* = Verifying Signed Source =
*
* Use SignedSource::isSigned() to determine if a file has a signature or not.
* Then, use SignedSource::verifySignature() to verify a file's signature:
*
* $is_signed = SignedSource::isSigned($questionable_file);
* if ($is_signed) {
* $intact = SignedSource::verifySignature($questionable_file);
* if ($intact) {
* echo 'File is signed with correct signature.';
* } else {
* echo 'File is signed with invalid signature. It has been edited!';
* }
* }
*
*/
final class SignedSource extends SignedSourceBase {
protected static function getTokenName() {
return 'generated';
}
/**
* Get the text for a doc block that can be used for an autogenerated file.
* If a comment is set, it will be included in the doc block.
*/
public static function getDocBlock(?string $comment = null) {
$comment = $comment ? $comment . "\n\n" : null;
return
"This file is generated. Do not modify it manually!\n\n".
$comment.
self::getSigningToken();
}
}