예제 #1
0
        private string _sign(byte[] data)
        {
            HMACSHA1 hmac = new HMACSHA1(Encoding.UTF8.GetBytes(this.SecretKey));

            byte[] digest = hmac.ComputeHash(data);
            return(StringUtils.urlSafeBase64Encode(digest));
        }
예제 #2
0
        public static string hash(string filePath)
        {
            string qetag = "";

            try
            {
                using (FileStream stream = new FileStream(filePath, FileMode.Open, FileAccess.Read))
                {
                    long   fileLength  = stream.Length;
                    byte[] buffer      = new byte[BLOCK_SIZE];
                    byte[] finalBuffer = new byte[BLOCK_SHA1_SIZE + 1];
                    if (fileLength <= BLOCK_SIZE)
                    {
                        int    readByteCount = stream.Read(buffer, 0, BLOCK_SIZE);
                        byte[] readBuffer    = new byte[readByteCount];
                        Array.Copy(buffer, readBuffer, readByteCount);

                        byte[] sha1Buffer = StringUtils.sha1(readBuffer);

                        finalBuffer[0] = 0x16;
                        Array.Copy(sha1Buffer, 0, finalBuffer, 1, sha1Buffer.Length);
                    }
                    else
                    {
                        long   blockCount    = (fileLength % BLOCK_SIZE == 0) ? (fileLength / BLOCK_SIZE) : (fileLength / BLOCK_SIZE + 1);
                        byte[] sha1AllBuffer = new byte[BLOCK_SHA1_SIZE * blockCount];

                        for (int i = 0; i < blockCount; i++)
                        {
                            int    readByteCount = stream.Read(buffer, 0, BLOCK_SIZE);
                            byte[] readBuffer    = new byte[readByteCount];
                            Array.Copy(buffer, readBuffer, readByteCount);

                            byte[] sha1Buffer = StringUtils.sha1(readBuffer);
                            Array.Copy(sha1Buffer, 0, sha1AllBuffer, i * BLOCK_SHA1_SIZE, sha1Buffer.Length);
                        }

                        byte[] sha1AllBufferSha1 = StringUtils.sha1(sha1AllBuffer);

                        finalBuffer[0] = 0x96;
                        Array.Copy(sha1AllBufferSha1, 0, finalBuffer, 1, sha1AllBufferSha1.Length);
                    }
                    qetag = StringUtils.urlSafeBase64Encode(finalBuffer);
                }
            }
            catch (Exception) { }

            return(qetag);
        }
예제 #3
0
        public string SignRequest(string url, byte[] reqBody)
        {
            Uri u = new Uri(url);

            using (HMACSHA1 hmac = new HMACSHA1(Encoding.UTF8.GetBytes(this.SecretKey)))
            {
                string pathAndQuery      = u.PathAndQuery;
                byte[] pathAndQueryBytes = Encoding.UTF8.GetBytes(pathAndQuery);
                using (MemoryStream buffer = new MemoryStream())
                {
                    buffer.Write(pathAndQueryBytes, 0, pathAndQueryBytes.Length);
                    buffer.WriteByte((byte)'\n');
                    if (reqBody != null && reqBody.Length > 0)
                    {
                        buffer.Write(reqBody, 0, reqBody.Length);
                    }
                    byte[] digest       = hmac.ComputeHash(buffer.ToArray());
                    string digestBase64 = StringUtils.urlSafeBase64Encode(digest);
                    return(string.Format("{0}:{1}", this.AccessKey, digestBase64));
                }
            }
        }
예제 #4
0
        public string SignWithData(byte[] data)
        {
            string encodedData = StringUtils.urlSafeBase64Encode(data);

            return(string.Format("{0}:{1}:{2}", this.AccessKey, this._sign(Encoding.UTF8.GetBytes(encodedData)), encodedData));
        }