@@ -62,7 +62,7 @@ public class RawEcdhKeyringExample
6262 private static async Task RawEcdhKeyringExampleGetItemPutItem ( )
6363 {
6464 var ddbTableName = TestUtils . TEST_DDB_TABLE_NAME ;
65-
65+
6666 // 1. Load key pair from UTF-8 encoded PEM files.
6767 // You may provide your own PEM files to use here.
6868 // If you do not, the main method in this class will generate PEM
@@ -77,7 +77,7 @@ private static async Task RawEcdhKeyringExampleGetItemPutItem()
7777 {
7878 throw new IOException ( "Exception while reading private key from file" , e ) ;
7979 }
80-
80+
8181 MemoryStream publicKeyRecipientUtf8EncodedByteBuffer ;
8282 try
8383 {
@@ -89,7 +89,7 @@ private static async Task RawEcdhKeyringExampleGetItemPutItem()
8989 {
9090 throw new IOException ( "Exception while reading public key from file" , e ) ;
9191 }
92-
92+
9393 // 2. Create the keyring.
9494 // The DynamoDb encryption client uses this to encrypt and decrypt items.
9595 var keyringInput = new CreateRawEcdhKeyringInput
@@ -108,21 +108,21 @@ private static async Task RawEcdhKeyringExampleGetItemPutItem()
108108 {
109109 // Must be a UTF8 PEM-encoded private key
110110 SenderStaticPrivateKey = privateKeySenderUtf8EncodedByteBuffer ,
111- // Must be a UTF8 DER-encoded X.509 public key also known as SubjectPublicKeyInfo.
111+ // Must be a UTF8 DER-encoded X.509 public key also known as SubjectPublicKeyInfo.
112112 RecipientPublicKey = publicKeyRecipientUtf8EncodedByteBuffer
113113 }
114114 }
115115 } ;
116116 var matProv = new MaterialProviders ( new MaterialProvidersConfig ( ) ) ;
117117 var rawEcdhKeyring = matProv . CreateRawEcdhKeyring ( keyringInput ) ;
118-
118+
119119 await PutGetExampleWithKeyring ( rawEcdhKeyring , ddbTableName ) ;
120120 }
121121
122122 private static async Task EphemeralRawEcdhKeyringPutItem ( )
123123 {
124124 var ddbTableName = TestUtils . TEST_DDB_TABLE_NAME ;
125-
125+
126126 // 1. Load key pair from UTF-8 encoded PEM files.
127127 // You may provide your own PEM files to use here.
128128 // If you do not, the RawEcdhKeyringExamples method in this class will generate PEM
@@ -138,7 +138,7 @@ private static async Task EphemeralRawEcdhKeyringPutItem()
138138 {
139139 throw new IOException ( "Exception while reading public key from file" , e ) ;
140140 }
141-
141+
142142 // 2. Create the keyring.
143143 // The DynamoDb encryption client uses this to encrypt and decrypt items.
144144 var keyringInput = new CreateRawEcdhKeyringInput
@@ -161,17 +161,17 @@ private static async Task EphemeralRawEcdhKeyringPutItem()
161161 } ;
162162 var matProv = new MaterialProviders ( new MaterialProvidersConfig ( ) ) ;
163163 var rawEcdhKeyring = matProv . CreateRawEcdhKeyring ( keyringInput ) ;
164-
164+
165165 // A raw ecdh keyring with Ephemeral configuration cannot decrypt data since the key pair
166166 // used as the sender is ephemeral. This means that at decrypt time it does not have
167167 // the private key that corresponds to the public key that is stored on the message.
168168 await PutExampleWithKeyring ( rawEcdhKeyring , ddbTableName ) ;
169169 }
170-
170+
171171 private static async Task DiscoveryRawEcdhKeyringGetItem ( )
172172 {
173173 var ddbTableName = TestUtils . TEST_DDB_TABLE_NAME ;
174-
174+
175175 // 1. Load key pair from UTF-8 encoded PEM files.
176176 // You may provide your own PEM files to use here.
177177 // If you do not, the main method in this class will generate PEM
@@ -186,7 +186,7 @@ private static async Task DiscoveryRawEcdhKeyringGetItem()
186186 {
187187 throw new IOException ( "Exception while reading private key from file" , e ) ;
188188 }
189-
189+
190190 // 2. Create the keyring.
191191 // The DynamoDb encryption client uses this to encrypt and decrypt items.
192192 var keyringInput = new CreateRawEcdhKeyringInput
@@ -210,7 +210,7 @@ private static async Task DiscoveryRawEcdhKeyringGetItem()
210210 } ;
211211 var matProv = new MaterialProviders ( new MaterialProvidersConfig ( ) ) ;
212212 var rawEcdhKeyring = matProv . CreateRawEcdhKeyring ( keyringInput ) ;
213-
213+
214214 await GetExampleWithKeyring ( rawEcdhKeyring , ddbTableName ) ;
215215 }
216216
@@ -403,7 +403,7 @@ private static async Task PutExampleWithKeyring(IKeyring rawEcdhKeyring, string
403403
404404 // Demonstrate that PutItem succeeded
405405 Debug . Assert ( putResponse . HttpStatusCode == HttpStatusCode . OK ) ;
406-
406+
407407 // 8. Try to get the item and assert that the ephemeral keyring configuration
408408 // cannot decrypt data.
409409 var keyToGet = new Dictionary < String , AttributeValue >
@@ -426,9 +426,9 @@ private static async Task PutExampleWithKeyring(IKeyring rawEcdhKeyring, string
426426 {
427427 Debug . Assert ( e . Message . Contains ( "EphemeralPrivateKeyToStaticPublicKey Key Agreement Scheme is forbidden on decrypt." ) ) ;
428428 }
429-
429+
430430 }
431-
431+
432432 private static async Task GetExampleWithKeyring ( IKeyring rawEcdhKeyring , string ddbTableName )
433433 {
434434 // 3. Configure which attributes are encrypted and/or signed when writing new items.
@@ -515,7 +515,7 @@ private static async Task GetExampleWithKeyring(IKeyring rawEcdhKeyring, string
515515 {
516516 Debug . Assert ( e . Message . Contains ( "PublicKeyDiscovery Key Agreement Scheme is forbidden on encrypt." ) ) ;
517517 }
518-
518+
519519 // 8. Get the item back from our table using the same client.
520520 // The client will decrypt the item client-side, and return
521521 // back the original item.
@@ -550,7 +550,7 @@ public static async Task RawEcdhKeyringExamples()
550550 {
551551 GenerateEccKeyPairs ( ) ;
552552 }
553-
553+
554554 await RawEcdhKeyringExampleGetItemPutItem ( ) ;
555555 await EphemeralRawEcdhKeyringPutItem ( ) ;
556556 await DiscoveryRawEcdhKeyringGetItem ( ) ;
@@ -560,44 +560,44 @@ public static async Task RawEcdhKeyringExamples()
560560 private static bool ShouldGenerateNewEccKeys ( )
561561 {
562562 // If keys already exists; do not overwrite existing keys.
563- if ( File . Exists ( EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER )
563+ if ( File . Exists ( EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER )
564564 && File . Exists ( EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT )
565565 && File . Exists ( EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT ) )
566566 {
567567 return false ;
568568 }
569569
570570 // If only two keys are present; throw exception
571- if ( ! File . Exists ( EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER )
571+ if ( ! File . Exists ( EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER )
572572 && File . Exists ( EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT )
573573 && File . Exists ( EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT ) )
574574 {
575575 throw new ApplicationException ( "Missing private key file at: " + EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER ) ;
576576 }
577-
577+
578578 // If only two keys are present; throw exception
579- if ( File . Exists ( EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER )
579+ if ( File . Exists ( EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER )
580580 && File . Exists ( EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT )
581581 && ! File . Exists ( EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT ) )
582582 {
583583 throw new ApplicationException ( "Missing private key file at: " + EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT ) ;
584584 }
585-
585+
586586 // If only two keys are present; throw exception
587- if ( File . Exists ( EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER )
587+ if ( File . Exists ( EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER )
588588 && ! File . Exists ( EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT )
589589 && File . Exists ( EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT ) )
590590 {
591591 throw new ApplicationException ( "Missing public key file at: " + EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT ) ;
592592 }
593-
593+
594594 return true ;
595595 }
596-
596+
597597 private static void GenerateEccKeyPairs ( )
598598 {
599599 // Safety check; Validate neither file is present
600- if ( File . Exists ( EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER )
600+ if ( File . Exists ( EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER )
601601 || File . Exists ( EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT )
602602 || File . Exists ( EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT ) )
603603 {
@@ -620,7 +620,7 @@ private static void GenerateEccKeyPairs()
620620 generator = new ECKeyPairGenerator ( ) ;
621621 SecureRandom rng = new SecureRandom ( ) ;
622622 X9ECParameters p = ECNamedCurveTable . GetByName ( "secp256r1" ) ;
623-
623+
624624 var domainParameters = new ECDomainParameters ( p . Curve , p . G , p . N , p . H ) ;
625625 generator . Init ( new ECKeyGenerationParameters ( domainParameters , rng ) ) ;
626626
@@ -630,10 +630,10 @@ private static void GenerateEccKeyPairs()
630630 Console . WriteLine ( e ) ;
631631 throw ;
632632 }
633-
633+
634634 AsymmetricCipherKeyPair senderKeyPair = generator . GenerateKeyPair ( ) ;
635635 AsymmetricCipherKeyPair recipientKeyPair = generator . GenerateKeyPair ( ) ;
636-
636+
637637 WritePrivateKey ( senderKeyPair . Private , EXAMPLE_ECC_PRIVATE_KEY_FILENAME_SENDER ) ;
638638 WritePrivateKey ( recipientKeyPair . Private , EXAMPLE_ECC_PRIVATE_KEY_FILENAME_RECIPIENT ) ;
639639 WritePublicKey ( recipientKeyPair , "secp256r1" , EXAMPLE_ECC_PUBLIC_KEY_FILENAME_RECIPIENT ) ;
@@ -650,19 +650,19 @@ private static void WritePrivateKey(AsymmetricKeyParameter privateKey, string fi
650650 fc . Write ( privateKeyUtf8EncodedBytes ) ;
651651 fc . Close ( ) ;
652652 }
653-
653+
654654 private static void WritePublicKey ( AsymmetricCipherKeyPair publicKey , string curveName , string fileName )
655655 {
656656 var ecdhCurveSpecFromCurveName = ToEcdhCurveSpec ( curveName ) ;
657657 var spki = KeyGeneration . SerializePublicKey ( publicKey , ecdhCurveSpecFromCurveName ) . CloneAsArray ( ) ;
658-
658+
659659 var publicKeyStringWriter = new StringWriter ( ) ;
660660 var publicKeyPemWriter = new PemWriter ( publicKeyStringWriter ) ;
661661 publicKeyPemWriter . WriteObject ( new PemObject ( "PUBLIC KEY" , spki ) ) ;
662662 var publicKeyUtf8EncodedBytes = Encoding . UTF8 . GetBytes ( publicKeyStringWriter . ToString ( ) ) ;
663663 var fc = new FileStream ( fileName , FileMode . Create , FileAccess . Write ) ;
664664 fc . Write ( publicKeyUtf8EncodedBytes ) ;
665- fc . Close ( ) ;
665+ fc . Close ( ) ;
666666 }
667667
668668 private static _IECDHCurveSpec ToEcdhCurveSpec ( string curveName )
0 commit comments