using System;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace trainav.web.Utils;

public static class StringExtensions {
	public static string Sha256(this string rawData) {
		// Create a SHA256   
		using var sha256Hash = SHA256.Create();
		// ComputeHash - returns byte array  
		var bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));
		// Convert byte array to a string   
		var builder = new StringBuilder();
		for (var i = 0; i < bytes.Length; i++)
			builder.Append(bytes[i].ToString("x2"));
		return builder.ToString();
	}

	public static string Base64Encode(this string plainText) {
		var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
		return Convert.ToBase64String(plainTextBytes);
	}

	public static string UrlEncode(this string plainText)      => HttpUtility.UrlEncode(plainText);
	public static string Delimit(this   string input, int max) => input.PadRight(max, ' ').Substring(0, max).TrimEnd();
}