1+ using System . Text ;
2+
3+ namespace FlowSynx . Plugins . ShaHashing . UnitTests ;
4+
5+ public class HashHelperTests
6+ {
7+ private static readonly byte [ ] TestInput = Encoding . UTF8 . GetBytes ( "hello world" ) ;
8+
9+ [ Theory ]
10+ [ InlineData ( "hello world" , "2aae6c35c94fcfb415dbe95f408b9ce91ee846ed" ) ] // SHA1
11+ public void SHA1_ShouldMatchExpectedHash ( string input , string expectedHex )
12+ {
13+ var actual = HashHelper . SHA1 ( Encoding . UTF8 . GetBytes ( input ) ) ;
14+ Assert . Equal ( expectedHex , ConvertToHex ( actual ) ) ;
15+ }
16+
17+ [ Theory ]
18+ [ InlineData ( "hello world" , "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9" ) ] // SHA256
19+ public void SHA256_ShouldMatchExpectedHash ( string input , string expectedHex )
20+ {
21+ var actual = HashHelper . SHA256 ( Encoding . UTF8 . GetBytes ( input ) ) ;
22+ Assert . Equal ( expectedHex , ConvertToHex ( actual ) ) ;
23+ }
24+
25+ [ Theory ]
26+ [ InlineData ( "hello world" , "fdbd8e75a67f29f701a4e040385e2e23986303ea10239211af907fcbb83578b3e417cb71ce646efd0819dd8c088de1bd" ) ] // SHA384
27+ public void SHA384_ShouldMatchExpectedHash ( string input , string expectedHex )
28+ {
29+ var actual = HashHelper . SHA384 ( Encoding . UTF8 . GetBytes ( input ) ) ;
30+ Assert . Equal ( expectedHex , ConvertToHex ( actual ) ) ;
31+ }
32+
33+ [ Theory ]
34+ [ InlineData ( "hello world" , "309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bcd3ca86d4cd86f989dd35bc5ff499670da34255b45b0cfd830e81f605dcf7dc5542e93ae9cd76f" ) ] // SHA512
35+ public void SHA512_ShouldMatchExpectedHash ( string input , string expectedHex )
36+ {
37+ var actual = HashHelper . SHA512 ( Encoding . UTF8 . GetBytes ( input ) ) ;
38+ Assert . Equal ( expectedHex , ConvertToHex ( actual ) ) ;
39+ }
40+
41+ [ Fact ]
42+ public void SHA3_256_ShouldProduceCorrectLength ( )
43+ {
44+ var hash = HashHelper . SHA3_256 ( TestInput ) ;
45+ Assert . Equal ( 32 , hash . Length ) ;
46+ }
47+
48+ [ Fact ]
49+ public void SHA3_384_ShouldProduceCorrectLength ( )
50+ {
51+ var hash = HashHelper . SHA3_384 ( TestInput ) ;
52+ Assert . Equal ( 48 , hash . Length ) ;
53+ }
54+
55+ [ Fact ]
56+ public void SHA3_512_ShouldProduceCorrectLength ( )
57+ {
58+ var hash = HashHelper . SHA3_512 ( TestInput ) ;
59+ Assert . Equal ( 64 , hash . Length ) ;
60+ }
61+
62+ [ Fact ]
63+ public void SHA224_ShouldProduceCorrectLength ( )
64+ {
65+ var hash = HashHelper . SHA224 ( TestInput ) ;
66+ Assert . Equal ( 28 , hash . Length ) ;
67+ }
68+
69+ [ Fact ]
70+ public void SHA512_224_ShouldProduceCorrectLength ( )
71+ {
72+ var hash = HashHelper . SHA512_224 ( TestInput ) ;
73+ Assert . Equal ( 28 , hash . Length ) ;
74+ }
75+
76+ [ Fact ]
77+ public void SHA512_256_ShouldProduceCorrectLength ( )
78+ {
79+ var hash = HashHelper . SHA512_256 ( TestInput ) ;
80+ Assert . Equal ( 32 , hash . Length ) ;
81+ }
82+
83+ [ Fact ]
84+ public void SHA3_224_ShouldProduceCorrectLength ( )
85+ {
86+ var hash = HashHelper . SHA3_224 ( TestInput ) ;
87+ Assert . Equal ( 28 , hash . Length ) ;
88+ }
89+
90+ [ Fact ]
91+ public void SHAKE128_ShouldProduceExactLength ( )
92+ {
93+ int len = 64 ;
94+ var hash = HashHelper . SHAKE128 ( TestInput , len ) ;
95+ Assert . Equal ( len , hash . Length ) ;
96+ }
97+
98+ [ Fact ]
99+ public void SHAKE256_ShouldProduceExactLength ( )
100+ {
101+ int len = 64 ;
102+ var hash = HashHelper . SHAKE256 ( TestInput , len ) ;
103+ Assert . Equal ( len , hash . Length ) ;
104+ }
105+
106+ private static string ConvertToHex ( byte [ ] data ) =>
107+ BitConverter . ToString ( data ) . Replace ( "-" , "" ) . ToLowerInvariant ( ) ;
108+ }
0 commit comments