Files
qrcodeService/QRCodeService/Controllers/PlaygroundController.cs
2021-02-23 09:51:50 +08:00

36 lines
1.1 KiB
C#

using Base62;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace QRCodeService.Controllers
{
[Route("[controller]/[action]")]
public class PlaygroundController:ControllerBase
{
public IActionResult GenShortCode(string url)
{
var codes = new string[2];
using (var md5 = MD5.Create())
{
var chunkSize = 8;
var result = md5.ComputeHash(Encoding.UTF8.GetBytes(url));
for (int i = 0; i < 2; i++)
{
var first = result.Take(chunkSize).ToArray();
//即超过30位的忽略处理
var res = BitConverter.ToInt64(first);
var code = res.ToBase62();
codes[i] = code;
result = result.Skip(chunkSize).ToArray();
}
return Ok(codes);
}
}
}
}