S3 Upload Policy : Encrypt Decrypt « Security « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# / C Sharp » Security » Encrypt DecryptScreenshots 
S3 Upload Policy
        
/******************************************************************************* 
 *  Copyright 2008 Amazon Technologies, Inc.
 *  Licensed under the Apache License, Version 2.0 (the "License"); 
 *  
 *  You may not use this file except in compliance with the License. 
 *  You may obtain a copy of the License at: http://aws.amazon.com/apache2.0
 *  This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 
 *  CONDITIONS OF ANY KIND, either express or implied. See the License for the 
 *  specific language governing permissions and limitations under the License.
 * ***************************************************************************** 
 *    __  _    _  ___ 
 *   (  )( \/\/ )/ __)
 *   /__\ \    / \__ \
 *  (_)(_) \/\/  (___/
 
 *  Amazon EC2 CSharp Library
 *  API Version: 2008-12-01
 *  Generated: Fri Dec 26 23:53:41 PST 2008 
 
 */

using System;
using System.Text;
using System.Security.Cryptography;
using System.Globalization;

namespace Amazon.EC2.Util
{

    /// <summary>
    /// This class represents S3 upload policy. Policy string
    /// representaion and signature to be used within EC2 bundling API.
    /// </summary>
    public class S3UploadPolicy
    {
        private String policySignature;
        private String policyString;

        /// <summary>
        /// S3 Upload policy to be used by EC2 API.
        /// </summary>
        /// <param name="awsAccessKeyId">Access Key Id of the signer of the policy</param>
        /// <param name="awsSecretKey">Secret Key of the signer of the policy</param>
        /// <param name="bucketName">Bucket name to upload</param>
        /// <param name="prefix">Prefix for the object keys</param>
        /// <param name="expireInMinutes">Expire, minutes from now</param>
        ///
        public S3UploadPolicy(String awsAccessKeyId,
                String awsSecretKey,
                String bucketName,
                String prefix,
                int expireInMinutes)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("{")
                    .Append("\"expiration\": \"")
                    .Append(GetFormattedTimestamp(expireInMinutes))
                    .Append("\",")
                    .Append("\"conditions\": [")
                    .Append("{\"bucket\": \"")
                    .Append(bucketName)
                    .Append("\"},")
                    .Append("{\"acl\": \"")
                    .Append("ec2-bundle-read")
                    .Append("\"},")
                    .Append("[\"starts-with\", \"$key\", \"")
                    .Append(prefix)
                    .Append("\"]")
                    .Append("]}");
            Encoding encoding = new UTF8Encoding();
            this.policyString = Convert.ToBase64String(encoding.GetBytes(builder.ToString().ToCharArray()));
            this.policySignature = SignPolicy(awsSecretKey, policyString);
        }

        /// <summary>
        /// Base64 representation of the serialized policy.
        /// Use policy generated by this method
        /// for passing to EC2 bunding calls.
        /// </summary>
        /// <returns>Base64 policy</returns>
        public String PolicyString
        {
            get
            {
                return this.policyString;
            }
        }

        /// <summary>
        /// Policy signature in base64 format
        /// Use signature generated by this method
        /// for passing to EC2 bunding calls along with policy.
        /// </summary>
        /// <returns>Base64 signature</returns>
        public String PolicySignature
        {
            get
            {
                return this.policySignature;
            }
        }

        private String SignPolicy(String awsSecretKey, String base64EncodedPolicy)
        {
            Encoding encoding = new UTF8Encoding();
            HMACSHA1 signature = new HMACSHA1(encoding.GetBytes(awsSecretKey));
            return Convert.ToBase64String(signature.ComputeHash(
                encoding.GetBytes(base64EncodedPolicy.ToCharArray())));
        }


        private String GetFormattedTimestamp(int minutesFromNow)
        {
            DateTime dateTime = DateTime.Now.AddMinutes(minutesFromNow);
            return new DateTime(dateTime.Year,
                                dateTime.Month,
                                dateTime.Day,
                                dateTime.Hour,
                                dateTime.Minute,
                                dateTime.Second,
                                dateTime.Millisecond,
                                DateTimeKind.Local)
                               .ToUniversalTime()
                               .ToString("yyyy-MM-dd\\THH:mm:ss.fff\\Z",
                                CultureInfo.InvariantCulture);
        }

    }
}

   
    
    
    
    
    
    
    
  
Related examples in the same category
1.Encrypt Utils
2.Decrypt Utils
3.Provides the Unix crypt() encryption algorithm.
4.Encrypts the value by password and salt.
5.Encrypt/Decrypt String To Bytes
6.Encrypt the given string using AES
7.Decrypt/Encrypt String AES
8.Encrypt String
9.Encrypt and Decrypt String
10.Encrypt a string
11.Crypto Utility
12.Crypto Utilities
13.Encryption Helper
14.Key Creator
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.