2017-09-29 03:06:07 +02:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2020-12-02 10:19:17 +02:00
|
|
|
using System.IO;
|
2017-09-29 03:06:07 +02:00
|
|
|
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";
|
|
|
|
|
2020-12-02 10:19:17 +02:00
|
|
|
var url = SignerHelper.GenerateUrl(Key, Salt, Url, Resize, Width, Height, Gravity, Enlarge, Extension);
|
2017-09-29 03:06:07 +02:00
|
|
|
|
|
|
|
Console.WriteLine(url);
|
|
|
|
}
|
2020-12-02 10:19:17 +02:00
|
|
|
}
|
2017-09-29 03:06:07 +02:00
|
|
|
|
2020-12-02 10:19:17 +02:00
|
|
|
public static class SignerHelper
|
|
|
|
{
|
|
|
|
public static string GenerateUrl(string key, string salt, string url, string resize, int width, int height, string gravity, int enlarge, string extension)
|
2017-09-29 03:06:07 +02:00
|
|
|
{
|
2020-12-02 10:19:17 +02:00
|
|
|
var keybin = HexadecimalStringToByteArray(key);
|
|
|
|
var saltBin = HexadecimalStringToByteArray(salt);
|
2017-09-29 03:06:07 +02:00
|
|
|
|
2020-12-02 10:19:17 +02:00
|
|
|
var encodedUrl = EncodeBase64URLSafeString(url);
|
2021-03-15 13:26:38 +02:00
|
|
|
var path = $"/rs:{resize}:{width}:{height}:{enlarge}/g:{gravity}/{encodedUrl}.{extension}";
|
2017-09-29 03:06:07 +02:00
|
|
|
|
2020-12-02 10:19:17 +02:00
|
|
|
var passwordWithSaltBytes = new List<byte>();
|
|
|
|
passwordWithSaltBytes.AddRange(saltBin);
|
|
|
|
passwordWithSaltBytes.AddRange(Encoding.UTF8.GetBytes(path));
|
2017-09-29 03:06:07 +02:00
|
|
|
|
2020-12-02 10:19:17 +02:00
|
|
|
using var hmac = new HMACSHA256(keybin);
|
|
|
|
byte[] digestBytes = hmac.ComputeHash(passwordWithSaltBytes.ToArray());
|
|
|
|
var urlSafeBase64 = EncodeBase64URLSafeString(digestBytes);
|
|
|
|
return $"/{urlSafeBase64}{path}";
|
2017-09-29 03:06:07 +02:00
|
|
|
}
|
|
|
|
|
2020-12-02 10:19:17 +02:00
|
|
|
static byte[] HexadecimalStringToByteArray(string input)
|
2017-09-29 03:06:07 +02:00
|
|
|
{
|
2020-12-02 10:19:17 +02:00
|
|
|
var outputLength = input.Length / 2;
|
|
|
|
var output = new byte[outputLength];
|
|
|
|
using (var sr = new StringReader(input))
|
|
|
|
{
|
|
|
|
for (var i = 0; i < outputLength; i++)
|
|
|
|
output[i] = Convert.ToByte(new string(new char[2] { (char)sr.Read(), (char)sr.Read() }), 16);
|
|
|
|
}
|
|
|
|
return output;
|
2017-09-29 03:06:07 +02:00
|
|
|
}
|
2020-12-02 10:19:17 +02:00
|
|
|
|
|
|
|
static string EncodeBase64URLSafeString(this byte[] stream)
|
|
|
|
=> Convert.ToBase64String(stream).TrimEnd('=').Replace('+', '-').Replace('/', '_');
|
|
|
|
|
|
|
|
static string EncodeBase64URLSafeString(this string str)
|
2022-08-26 13:38:27 +02:00
|
|
|
=> EncodeBase64URLSafeString(Encoding.UTF8.GetBytes(str));
|
2017-09-29 03:06:07 +02:00
|
|
|
}
|
|
|
|
}
|