1 #Region "Microsoft.VisualBasic::54168908a8a088c83e19233d15cbb460, Microsoft.VisualBasic.Core\ApplicationServices\VBDev\Signature\CodeSign.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Module CodeSign
35     
36     
37     '         Enum Languages
38     
39     '             PHP, R, TypeScript, VisualBasic
40     
41     
42     
43     '  
44     
45     '     Function: ParseHeaderRegion, SignCode
46     
47     
48     ' /********************************************************************************/
49
50 #End Region
51
52 Imports Microsoft.VisualBasic.SecurityString
53 Imports r = System.Text.RegularExpressions.Regex
54
55 Namespace ApplicationServices.Development
56
57     ''' <summary>
58     ''' 使用AES加密进行代码的签名操作
59     ''' </summary>
60     Public Module CodeSign
61
62         Public Enum Languages
63             VisualBasic
64             R
65             PHP
66             TypeScript
67         End Enum
68
69         Const PhpHeaderRegion$ = "#region ""PHP\\Foundation[:]{2}.*"".+?#endregion"
70         Const TypeScriptHeaderRegion$ = "//#region ""Microsoft.TypeScript[:]{2}.*"".+?//#endregion"
71         Const ROpenHeaderRegion$ = "#Region ""Microsoft.ROpen[:]{2}.*"".+?#End Region"
72         Const VisualBasicHeaderRegion$ = "#Region ""Microsoft.VisualBasic[:]{2}.*"".+?#End Region"
73
74         ReadOnly regionPatterns As New Dictionary(Of Languages, String) From {
75             {Languages.PHP, PhpHeaderRegion},
76             {Languages.R, ROpenHeaderRegion},
77             {Languages.TypeScript, TypeScriptHeaderRegion},
78             {Languages.VisualBasic, VisualBasicHeaderRegion}
79         }
80
81         ''' <summary>
82         ''' 这个函数对代码部分的文本生成md5,然后将md5值字符串使用自己的密匙加密,后将结果写在代码文档的header region之中
83         ''' 在验证的时候,只需要取出加密的结果,使用公匙解密,得到md5值,然后再计算代码部分的md5,二者比较一下就能够了解代
84         ''' 码是否被修改了
85         ''' </summary>
86         ''' <param name="code$"></param>
87         ''' <param name="key"></param>
88         ''' <param name="lang"></param>
89         ''' <returns></returns>
90         Public Function SignCode(code$, key As AES, Optional lang As Languages = Languages.VisualBasic) As String
91             Dim region$ = ParseHeaderRegion(code, lang)
92             Dim plainCode$ = code.Trim.Substring(region.Length + 1).Trim
93             Dim md5$ = plainCode.MD5
94             ' 使用自己的密匙加密md5,得到了待验证的数据
95             Dim sign = key.EncryptData(md5)
96
97             Throw New NotImplementedException
98         End Function
99
100         Public Function ParseHeaderRegion(code$, lang As Languages) As String
101             Return r.Match(code, regionPatterns(lang), RegexICSng).Value
102         End Function
103     End Module
104 End Namespace