@@ -18,45 +18,45 @@ public static class Crypto
1818 public static uint rotr ( uint n , int d ) => ( n >> d ) | ( n << ( 32 - d ) ) ;
1919
2020 public static byte [ ] EncryptAndDigest ( string aad , byte [ ] data ) {
21- byte [ ] nonce = Utilities . RandomBytes ( 12 ) ;
22- byte [ ] aadBytes = Encoding . UTF8 . GetBytes ( aad ) ;
23- byte [ ] aadHash = new SHA256Managed ( ) . ComputeHash ( aadBytes ) ;
24- var cipher = new GcmBlockCipher ( new AesEngine ( ) ) ;
25- var cParams = new AeadParameters ( new KeyParameter ( aadHash , 0 , 16 ) , 128 , nonce , aadBytes ) ;
26- cipher . Init ( true , cParams ) ;
27- byte [ ] ciphertext = new byte [ cipher . GetOutputSize ( data . Length ) ] ;
28- int retLen = cipher . ProcessBytes ( data , 0 , data . Length , ciphertext , 0 ) ;
29- cipher . DoFinal ( ciphertext , retLen ) ;
30-
31- byte [ ] output = new byte [ nonce . Length + ciphertext . Length ] ;
32- Buffer . BlockCopy ( nonce , 0 , output , 0 , nonce . Length ) ;
33- Buffer . BlockCopy ( ciphertext , 0 , output , nonce . Length , ciphertext . Length ) ;
34- return output ;
35- }
36-
37- public static byte [ ] DecryptAndVerify ( string aad , byte [ ] data ) {
38- byte [ ] nonce = new byte [ 12 ] ;
39- byte [ ] ciphertext = new byte [ data . Length - 12 ] ;
40- Buffer . BlockCopy ( data , 0 , nonce , 0 , nonce . Length ) ;
41- Buffer . BlockCopy ( data , nonce . Length , ciphertext , 0 , ciphertext . Length ) ;
42-
43- byte [ ] aadBytes = Encoding . UTF8 . GetBytes ( aad ) ;
44- byte [ ] aadHash = new SHA256Managed ( ) . ComputeHash ( aadBytes ) ;
45- var cipher = new GcmBlockCipher ( new AesEngine ( ) ) ;
46- var cParams = new AeadParameters ( new KeyParameter ( aadHash , 0 , 16 ) , 128 , nonce , aadBytes ) ;
47- cipher . Init ( false , cParams ) ;
48- byte [ ] plaintext = new byte [ cipher . GetOutputSize ( ciphertext . Length ) ] ;
49- int retLen = cipher . ProcessBytes ( ciphertext , 0 , ciphertext . Length , plaintext , 0 ) ;
50- cipher . DoFinal ( plaintext , retLen ) ;
51-
52- return plaintext ;
53- }
21+ byte [ ] nonce = Utilities . RandomBytes ( 12 ) ;
22+ byte [ ] aadBytes = Encoding . UTF8 . GetBytes ( aad ) ;
23+ byte [ ] aadHash = new SHA256Managed ( ) . ComputeHash ( aadBytes ) ;
24+ var cipher = new GcmBlockCipher ( new AesEngine ( ) ) ;
25+ var cParams = new AeadParameters ( new KeyParameter ( aadHash , 0 , 16 ) , 128 , nonce , aadBytes ) ;
26+ cipher . Init ( true , cParams ) ;
27+ byte [ ] ciphertext = new byte [ cipher . GetOutputSize ( data . Length ) ] ;
28+ int retLen = cipher . ProcessBytes ( data , 0 , data . Length , ciphertext , 0 ) ;
29+ cipher . DoFinal ( ciphertext , retLen ) ;
30+
31+ byte [ ] output = new byte [ nonce . Length + ciphertext . Length ] ;
32+ Buffer . BlockCopy ( nonce , 0 , output , 0 , nonce . Length ) ;
33+ Buffer . BlockCopy ( ciphertext , 0 , output , nonce . Length , ciphertext . Length ) ;
34+ return output ;
35+ }
36+
37+ public static byte [ ] DecryptAndVerify ( string aad , byte [ ] data ) {
38+ byte [ ] nonce = new byte [ 12 ] ;
39+ byte [ ] ciphertext = new byte [ data . Length - 12 ] ;
40+ Buffer . BlockCopy ( data , 0 , nonce , 0 , nonce . Length ) ;
41+ Buffer . BlockCopy ( data , nonce . Length , ciphertext , 0 , ciphertext . Length ) ;
42+
43+ byte [ ] aadBytes = Encoding . UTF8 . GetBytes ( aad ) ;
44+ byte [ ] aadHash = new SHA256Managed ( ) . ComputeHash ( aadBytes ) ;
45+ var cipher = new GcmBlockCipher ( new AesEngine ( ) ) ;
46+ var cParams = new AeadParameters ( new KeyParameter ( aadHash , 0 , 16 ) , 128 , nonce , aadBytes ) ;
47+ cipher . Init ( false , cParams ) ;
48+ byte [ ] plaintext = new byte [ cipher . GetOutputSize ( ciphertext . Length ) ] ;
49+ int retLen = cipher . ProcessBytes ( ciphertext , 0 , ciphertext . Length , plaintext , 0 ) ;
50+ cipher . DoFinal ( plaintext , retLen ) ;
51+
52+ return plaintext ;
53+ }
5454
5555 public static byte [ ] CalcProfileHash ( byte [ ] data ) {
5656 byte [ ] pfData = new byte [ data . Length - 0xC ] ;
5757 Buffer . BlockCopy ( data , 0xC , pfData , 0 , pfData . Length ) ;
5858
59- return FarmHash_Hash32 ( pfData ) ;
59+ return Hash32 ( pfData ) ;
6060 }
6161
6262 private static uint Mur ( uint a , uint h ) {
@@ -71,7 +71,7 @@ private static uint Mur(uint a, uint h) {
7171 return h * 5 + C3 ;
7272 }
7373
74- public static byte [ ] FarmHash_Hash32 ( byte [ ] data ) {
74+ public static byte [ ] Hash32 ( byte [ ] data ) {
7575 // Taken from https://github.com/google/farmhash/blob/master/src/farmhash.cc
7676
7777 uint size = ( uint ) data . Length ;
0 commit comments