2525using Amazon . KeyManagementService . Model ;
2626using Amazon . Runtime ;
2727using Amazon . S3 . Model ;
28- using ThirdParty . Json . LitJson ;
2928
3029namespace Amazon . Extensions . S3 . Encryption
3130{
@@ -276,7 +275,7 @@ internal static void UpdateMetadataWithEncryptionInstructionsV2(AmazonWebService
276275 metadata . Add ( XAmzKeyV2 , base64EncodedEnvelopeKey ) ;
277276 metadata . Add ( XAmzCekAlg , instructions . CekAlgorithm ) ;
278277 metadata . Add ( XAmzIV , base64EncodedIv ) ;
279- metadata . Add ( XAmzMatDesc , JsonMapper . ToJson ( instructions . MaterialsDescription ) ) ;
278+ metadata . Add ( XAmzMatDesc , JsonUtils . ToJson ( instructions . MaterialsDescription ) ) ;
280279 }
281280 }
282281
@@ -288,17 +287,17 @@ internal static PutObjectRequest CreateInstructionFileRequestV2(AmazonWebService
288287 var ivToStoreInInstructionFile = instructions . InitializationVector ;
289288 var base64EncodedIv = Convert . ToBase64String ( ivToStoreInInstructionFile ) ;
290289
291- var jsonData = new JsonData
290+ var keyValuePairs = new Dictionary < string , string > ( )
292291 {
293- [ XAmzTagLen ] = DefaultTagBitsLength . ToString ( ) ,
294- [ XAmzKeyV2 ] = base64EncodedEnvelopeKey ,
295- [ XAmzCekAlg ] = instructions . CekAlgorithm ,
296- [ XAmzWrapAlg ] = instructions . WrapAlgorithm ,
297- [ XAmzIV ] = base64EncodedIv ,
298- [ XAmzMatDesc ] = JsonMapper . ToJson ( instructions . MaterialsDescription )
292+ { XAmzTagLen , DefaultTagBitsLength . ToString ( ) } ,
293+ { XAmzKeyV2 , base64EncodedEnvelopeKey } ,
294+ { XAmzCekAlg , instructions . CekAlgorithm } ,
295+ { XAmzWrapAlg , instructions . WrapAlgorithm } ,
296+ { XAmzIV , base64EncodedIv } ,
297+ { XAmzMatDesc , JsonUtils . ToJson ( instructions . MaterialsDescription ) } ,
299298 } ;
300299
301- var contentBody = jsonData . ToJson ( ) ;
300+ var contentBody = JsonUtils . ToJson ( keyValuePairs ) ;
302301
303302 var putObjectRequest = request as PutObjectRequest ;
304303 if ( putObjectRequest != null )
@@ -451,7 +450,7 @@ internal static Dictionary<string, string> GetMaterialDescriptionFromMetaData(Me
451450 return new Dictionary < string , string > ( ) ;
452451 }
453452
454- var materialDescription = JsonMapper . ToObject < Dictionary < string , string > > ( materialDescriptionJsonString ) ;
453+ var materialDescription = JsonUtils . ToDictionary ( materialDescriptionJsonString ) ;
455454 return materialDescription ;
456455 }
457456
@@ -492,45 +491,45 @@ internal static EncryptionInstructions BuildInstructionsUsingInstructionFileV2(G
492491 {
493492 using ( TextReader textReader = new StreamReader ( response . ResponseStream ) )
494493 {
495- var jsonData = JsonMapper . ToObject ( textReader ) ;
494+ var keyValuePair = JsonUtils . ToDictionary ( textReader . ReadToEnd ( ) ) ;
496495
497- if ( jsonData [ XAmzKeyV2 ] != null )
496+ if ( keyValuePair . ContainsKey ( XAmzKeyV2 ) )
498497 {
499498 // The envelope contains data in V2 format
500- var encryptedEnvelopeKey = Base64DecodedDataValue ( jsonData , XAmzKeyV2 ) ;
499+ var encryptedEnvelopeKey = Base64DecodedDataValue ( keyValuePair , XAmzKeyV2 ) ;
501500 var decryptedEnvelopeKey = DecryptNonKmsEnvelopeKeyV2 ( encryptedEnvelopeKey , materials ) ;
502501
503- var initializationVector = Base64DecodedDataValue ( jsonData , XAmzIV ) ;
504- var materialDescription = JsonMapper . ToObject < Dictionary < string , string > > ( ( string ) jsonData [ XAmzMatDesc ] ) ;
502+ var initializationVector = Base64DecodedDataValue ( keyValuePair , XAmzIV ) ;
503+ var materialDescription = JsonUtils . ToDictionary ( ( string ) keyValuePair [ XAmzMatDesc ] ) ;
505504
506- var cekAlgorithm = StringValue ( jsonData , XAmzCekAlg ) ;
507- var wrapAlgorithm = StringValue ( jsonData , XAmzWrapAlg ) ;
505+ var cekAlgorithm = StringValue ( keyValuePair , XAmzCekAlg ) ;
506+ var wrapAlgorithm = StringValue ( keyValuePair , XAmzWrapAlg ) ;
508507
509508 var instructions = new EncryptionInstructions ( materialDescription , decryptedEnvelopeKey , null ,
510509 initializationVector , wrapAlgorithm , cekAlgorithm ) ;
511510
512511 return instructions ;
513512 }
514- else if ( jsonData [ XAmzKey ] != null )
513+ else if ( keyValuePair . ContainsKey ( XAmzKey ) )
515514 {
516515 // The envelope contains data in V1 format
517- var encryptedEnvelopeKey = Base64DecodedDataValue ( jsonData , XAmzKey ) ;
516+ var encryptedEnvelopeKey = Base64DecodedDataValue ( keyValuePair , XAmzKey ) ;
518517 var decryptedEnvelopeKey = DecryptNonKMSEnvelopeKey ( encryptedEnvelopeKey , materials ) ;
519518
520- var initializationVector = Base64DecodedDataValue ( jsonData , XAmzIV ) ;
521- var materialDescription = JsonMapper . ToObject < Dictionary < string , string > > ( ( string ) jsonData [ XAmzMatDesc ] ) ;
519+ var initializationVector = Base64DecodedDataValue ( keyValuePair , XAmzIV ) ;
520+ var materialDescription = JsonUtils . ToDictionary ( ( string ) keyValuePair [ XAmzMatDesc ] ) ;
522521
523522 var instructions = new EncryptionInstructions ( materialDescription , decryptedEnvelopeKey , null , initializationVector ) ;
524523
525524 return instructions ;
526525 }
527- else if ( jsonData [ EncryptedEnvelopeKey ] != null )
526+ else if ( keyValuePair . ContainsKey ( EncryptedEnvelopeKey ) )
528527 {
529528 // The envelope contains data in older format
530- var encryptedEnvelopeKey = Base64DecodedDataValue ( jsonData , EncryptedEnvelopeKey ) ;
529+ var encryptedEnvelopeKey = Base64DecodedDataValue ( keyValuePair , EncryptedEnvelopeKey ) ;
531530 var decryptedEnvelopeKey = DecryptNonKMSEnvelopeKey ( encryptedEnvelopeKey , materials ) ;
532531
533- var initializationVector = Base64DecodedDataValue ( jsonData , IV ) ;
532+ var initializationVector = Base64DecodedDataValue ( keyValuePair , IV ) ;
534533
535534 return new EncryptionInstructions ( materials . MaterialsDescription , decryptedEnvelopeKey , initializationVector ) ;
536535 }
@@ -541,24 +540,24 @@ internal static EncryptionInstructions BuildInstructionsUsingInstructionFileV2(G
541540 }
542541 }
543542
544- private static byte [ ] Base64DecodedDataValue ( JsonData jsonData , string key )
543+ private static byte [ ] Base64DecodedDataValue ( Dictionary < string , string > keyValuePairs , string key )
545544 {
546- var base64EncodedValue = jsonData [ key ] ;
547- if ( base64EncodedValue == null )
545+ if ( ! keyValuePairs . TryGetValue ( key , out var base64EncodedValue ) )
548546 {
549547 throw new ArgumentNullException ( nameof ( key ) ) ;
550548 }
549+
551550 return Convert . FromBase64String ( ( string ) base64EncodedValue ) ;
552551 }
553552
554- private static string StringValue ( JsonData jsonData , string key )
553+ private static string StringValue ( Dictionary < string , string > keyValuePairs , string key )
555554 {
556- var stringValue = jsonData [ key ] ;
557- if ( stringValue == null )
555+ if ( ! keyValuePairs . TryGetValue ( key , out var stringValue ) )
558556 {
559557 throw new ArgumentNullException ( nameof ( key ) ) ;
560558 }
561- return ( string ) stringValue ;
559+
560+ return stringValue ;
562561 }
563562 }
564563}
0 commit comments