-
-
Notifications
You must be signed in to change notification settings - Fork 629
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Brendan McKenzie
committed
Sep 29, 2017
1 parent
aa4e673
commit 0fed6b8
Showing
1 changed file
with
60 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Security.Cryptography; | ||
using System.Text; | ||
|
||
namespace ImgProxy.Examples | ||
{ | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
const string Key = "943b421c9eb07c830af81030552c86009268de4e532ba2ee2eab8247c6da0881"; | ||
const string Salt = "520f986b998545b4785e0defbc4f3c1203f22de2374a3d53cb7a7fe9fea309c5"; | ||
const string Url = "http://img.example.com/pretty/image.jpg"; | ||
|
||
const string Resize = "fill"; | ||
const int Width = 300; | ||
const int Height = 300; | ||
const string Gravity = "no"; | ||
const int Enlarge = 1; | ||
const string Extension = "png"; | ||
|
||
var url = GenerateUrl(Key, Salt, Url, Resize, Width, Height, Gravity, Enlarge, Extension); | ||
|
||
Console.WriteLine(url); | ||
} | ||
|
||
static string GenerateUrl(string key, string salt, string url, string resize, int width, int height, string gravity, int enlarge, string extension) | ||
{ | ||
var keybin = StringToByteArray(key); | ||
var saltBin = StringToByteArray(salt); | ||
|
||
var encodedUrl = string.Join("/", WholeChunks(Convert.ToBase64String(Encoding.UTF8.GetBytes(url)).TrimEnd('='), 16)); | ||
|
||
var path = $"/{resize}/{width}/{height}/{gravity}/{enlarge}/{encodedUrl}.{extension}"; | ||
|
||
using (var hmac = new HMACSHA256(keybin)) | ||
{ | ||
var hash = hmac.ComputeHash(saltBin.Concat(Encoding.UTF8.GetBytes(path)).ToArray()); | ||
|
||
return $"/{Convert.ToBase64String(hash).TrimEnd('=')}{path}"; | ||
} | ||
} | ||
|
||
static byte[] StringToByteArray(string hex) | ||
{ | ||
return Enumerable.Range(0, hex.Length) | ||
.Where(x => x % 2 == 0) | ||
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16)) | ||
.ToArray(); | ||
} | ||
|
||
static IEnumerable<string> WholeChunks(string str, int maxChunkSize) | ||
{ | ||
for (int i = 0; i < str.Length; i += maxChunkSize) | ||
yield return str.Substring(i, Math.Min(maxChunkSize, str.Length - i)); | ||
} | ||
} | ||
} |