1- using Algorithm . Interop . Exceptions ;
1+ using Common . Algorithm . Interop . Exceptions ;
22using System . Runtime . InteropServices ;
33using System . Text ;
44
5- namespace Algorithm . Interop
5+ namespace Common . Algorithm . Interop
66{
77 /// <summary>
88 /// 文件哈希类
99 /// </summary>
10- public class Hash
10+ public static class Hash
1111 {
1212 /// <summary>
1313 /// DLL 路径
1414 /// </summary>
15- private const string dll_path = Environment . dll_path ;
15+ private const string DllPath = Environment . DllPath ;
1616
1717 #region 扩展库函数导入 [DLLImport Hash.dll]
1818
1919 /// <summary>
2020 /// 调用扩展库进行 Hash
2121 /// </summary>
2222 /// <param name="src">元数据</param>
23+ /// <param name="output">输出</param>
24+ /// <param name="length">长度</param>
2325 /// <returns>哈希后的数据</returns>
24- [ DllImport ( $ "{ dll_path } Hash.dll", EntryPoint = "hash_str" ) ]
26+ [ DllImport ( $ "{ DllPath } Hash.dll", EntryPoint = "hash_str" ) ]
2527 private static extern void hash_str ( byte [ ] src , byte [ ] output , int length ) ;
2628
2729 /// <summary>
2830 /// 调用扩展库进行 Hash 压缩
2931 /// </summary>
3032 /// <param name="src">哈希数据</param>
33+ /// <param name="output">输出</param>
3134 /// <returns>哈希压缩后的数据</returns>
32- [ DllImport ( $ "{ dll_path } Hash.dll", EntryPoint = "hash_compress_128_str" ) ]
35+ [ DllImport ( $ "{ DllPath } Hash.dll", EntryPoint = "hash_compress_128_str" ) ]
3336 private static extern void hash_compress_128_str ( byte [ ] src , byte [ ] output ) ;
3437
3538 /// <summary>
3639 /// 调用扩展库进行 Hash 压缩
3740 /// </summary>
3841 /// <param name="src">哈希数据</param>
42+ /// <param name="output">输出</param>
3943 /// <returns>哈希压缩后的数据</returns>
40- [ DllImport ( $ "{ dll_path } Hash.dll", EntryPoint = "hash_compress_64_str" ) ]
44+ [ DllImport ( $ "{ DllPath } Hash.dll", EntryPoint = "hash_compress_64_str" ) ]
4145 private static extern void hash_compress_64_str ( byte [ ] src , byte [ ] output ) ;
4246
4347 /// <summary>
4448 /// 调用扩展库进行 Hash 压缩
4549 /// </summary>
4650 /// <param name="src">哈希数据</param>
51+ /// <param name="output">输出</param>
4752 /// <returns>哈希压缩后的数据</returns>
48- [ DllImport ( $ "{ dll_path } Hash.dll", EntryPoint = "hash_compress_32_str" ) ]
53+ [ DllImport ( $ "{ DllPath } Hash.dll", EntryPoint = "hash_compress_32_str" ) ]
4954 private static extern void hash_compress_32_str ( byte [ ] src , byte [ ] output ) ;
5055
5156 /// <summary>
5257 /// 调用扩展库进行 Hash 压缩
5358 /// </summary>
5459 /// <param name="src">哈希数据</param>
60+ /// <param name="output">输出</param>
5561 /// <returns>哈希压缩后的数据</returns>
56- [ DllImport ( $ "{ dll_path } Hash.dll", EntryPoint = "hash_compress_16_str" ) ]
62+ [ DllImport ( $ "{ DllPath } Hash.dll", EntryPoint = "hash_compress_16_str" ) ]
5763 private static extern void hash_compress_16_str ( byte [ ] src , byte [ ] output ) ;
5864
5965 /// <summary>
6066 /// 调用扩展库进行 Hash 压缩
6167 /// </summary>
6268 /// <param name="src">哈希数据</param>
69+ /// <param name="output">输出</param>
6370 /// <returns>哈希压缩后的数据</returns>
64- [ DllImport ( $ "{ dll_path } Hash.dll", EntryPoint = "hash_compress_8_str" ) ]
71+ [ DllImport ( $ "{ DllPath } Hash.dll", EntryPoint = "hash_compress_8_str" ) ]
6572 private static extern void hash_compress_8_str ( byte [ ] src , byte [ ] output ) ;
6673
6774 /// <summary>
6875 /// 调用扩展库进行 Hash 压缩
6976 /// </summary>
7077 /// <param name="src">哈希数据</param>
78+ /// <param name="output">输出</param>
7179 /// <returns>哈希压缩后的数据</returns>
72- [ DllImport ( $ "{ dll_path } Hash.dll", EntryPoint = "hash_compress_4_str" ) ]
80+ [ DllImport ( $ "{ DllPath } Hash.dll", EntryPoint = "hash_compress_4_str" ) ]
7381 private static extern void hash_compress_4_str ( byte [ ] src , byte [ ] output ) ;
7482
7583 #endregion
@@ -80,34 +88,33 @@ public class Hash
8088 /// 统一调用哈希压缩
8189 /// </summary>
8290 /// <param name="mid">源压缩中间哈希值</param>
83- /// <param name="rst">哈希压缩值储存地址</param>
8491 /// <param name="clv">哈希压缩级别</param>
8592 private static byte [ ] Compress ( ref byte [ ] mid , CompressLevel clv )
8693 {
8794 switch ( clv )
8895 {
8996 case CompressLevel . x128 :
90- byte [ ] ans128 = new byte [ 128 ] ;
97+ var ans128 = new byte [ 128 ] ;
9198 hash_compress_128_str ( mid , ans128 ) ;
9299 return ans128 ;
93100 case CompressLevel . x64 :
94- byte [ ] ans64 = new byte [ 64 ] ;
101+ var ans64 = new byte [ 64 ] ;
95102 hash_compress_64_str ( mid , ans64 ) ;
96103 return ans64 ;
97104 case CompressLevel . x32 :
98- byte [ ] ans32 = new byte [ 32 ] ;
105+ var ans32 = new byte [ 32 ] ;
99106 hash_compress_32_str ( mid , ans32 ) ;
100107 return ans32 ;
101108 case CompressLevel . x16 :
102- byte [ ] ans16 = new byte [ 16 ] ;
109+ var ans16 = new byte [ 16 ] ;
103110 hash_compress_16_str ( mid , ans16 ) ;
104111 return ans16 ;
105112 case CompressLevel . x8 :
106- byte [ ] ans8 = new byte [ 8 ] ;
113+ var ans8 = new byte [ 8 ] ;
107114 hash_compress_8_str ( mid , ans8 ) ;
108115 return ans8 ;
109116 case CompressLevel . x4 :
110- byte [ ] ans4 = new byte [ 4 ] ;
117+ var ans4 = new byte [ 4 ] ;
111118 hash_compress_4_str ( mid , ans4 ) ;
112119 return ans4 ;
113120 default : return Array . Empty < byte > ( ) ;
@@ -139,14 +146,15 @@ public static CompressLevel HashCompressLevelParse(string compressed,
139146 /// 进行字符串哈希
140147 /// </summary>
141148 /// <param name="str">字符串</param>
149+ /// <param name="clv">压缩级别</param>
142150 /// <returns>哈希后的Byte数组</returns>
143151 public static byte [ ] FromString ( string str ,
144152 CompressLevel clv = CompressLevel . x64 )
145153 {
146- byte [ ] array = Encoding . UTF8 . GetBytes ( str ) ; // 源字符串转 byte[]
147- byte [ ] mid = new byte [ 2048 ] ; // 存储哈希值
154+ var array = Encoding . UTF8 . GetBytes ( str ) ; // 源字符串转 byte[]
155+ var mid = new byte [ 2048 ] ; // 存储哈希值
148156 hash_str ( array , mid , array . Length ) ; // 哈希运算
149- byte [ ] rst = Compress ( ref mid , clv ) ; // 哈希压缩运算
157+ var rst = Compress ( ref mid , clv ) ; // 哈希压缩运算
150158 return rst ; // 返回哈希压缩值
151159 }
152160
@@ -155,15 +163,16 @@ public static byte[] FromString(string str,
155163 /// </summary>
156164 /// <param name="str">字符串</param>
157165 /// <param name="rmLink">是否移除连字符</param>
166+ /// <param name="clv">压缩级别</param>
158167 /// <returns>十六进制哈希字符串</returns>
159168 public static string FromString2Hex ( string str , bool rmLink = false ,
160169 CompressLevel clv = CompressLevel . x64 )
161170 {
162- byte [ ] array = Encoding . UTF8 . GetBytes ( str ) ; // 源字符串转 byte[]
163- byte [ ] mid = new byte [ 2048 ] ; // 存储哈希值
171+ var array = Encoding . UTF8 . GetBytes ( str ) ; // 源字符串转 byte[]
172+ var mid = new byte [ 2048 ] ; // 存储哈希值
164173 hash_str ( array , mid , array . Length ) ; // 哈希运算
165- byte [ ] rst = Compress ( ref mid , clv ) ; // 哈希压缩运算
166- string ans = BitConverter . ToString ( rst ) ; // 哈希压缩运算转十六进制字符串
174+ var rst = Compress ( ref mid , clv ) ; // 哈希压缩运算
175+ var ans = BitConverter . ToString ( rst ) ; // 哈希压缩运算转十六进制字符串
167176 return rmLink ? ans . Replace ( "-" , "" ) : ans ; // 返回字符串, 据参数删除连字符
168177 }
169178
@@ -215,7 +224,9 @@ public enum HashMode
215224 /// </summary>
216225 public enum CompressLevel
217226 {
227+ // ReSharper disable InconsistentNaming
218228 x128 = 5 , x64 = 4 , x32 = 3 , x16 = 2 , x8 = 1 , x4 = 0
229+ // ReSharper restore InconsistentNaming
219230 }
220231
221232 #endregion
0 commit comments