0

I have created a method in c# that takes unique email as input and returns base64 string. I am using MD5CryptoServiceProvider for this purpose. Here is the code:

public string GenerateHash(string str)
    {
        str = str.ToUpperInvariant();
        MD5 md5 = new MD5CryptoServiceProvider();
        byte[] emailBytes = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
        var base64Email = Convert.ToBase64String(emailBytes, 0, emailBytes.Length);
        return RemoveSpecialCharacters(base64Email);
    }

    private static string RemoveSpecialCharacters(string str)
    {
        return Regex.Replace(str, "[^a-zA-Z0-9_.]+", "", RegexOptions.Compiled);
    }

But the issue is it returns a string with special characters (eg /, ==) and I have to remove those special characters by myself using regex or something else but is there anyway I get a unique string without special characters so I don't have to use regex myself. Also is there any possibility I get a string of no more than 12 characters

Ask
  • 3,076
  • 6
  • 30
  • 63
  • there might be another special character too. I want to restrict all the special characters even (. and _) – Ask May 29 '18 at 07:23
  • How about converting the byte[] (base64 string) to hex? – usselite May 29 '18 at 07:24
  • You can convert hash array to [hex string](https://stackoverflow.com/q/311165/4685428) – Aleks Andreev May 29 '18 at 07:24
  • If you don't mind the extra length, the hexadecimal representation of the MD5 is 32 digits of only `0-9` and `A-F` characters. – C.Evenhuis May 29 '18 at 07:24
  • I have edited my post, can you tell me now? – Ask May 29 '18 at 07:28
  • No, I can't affort extra length – Ask May 29 '18 at 07:29
  • 3
    @Ask - If you can't afford extra length then you can't get rid of special characters. They enable the encoding to be shorter. If you restrict the encoding alphabet then the length must increase. – Enigmativity May 29 '18 at 07:34
  • @Ask - Of course, if you're happy to throw away information you can make them as short as you like. – Enigmativity May 29 '18 at 07:38
  • @Ask - Also, please make sure you post real code. As it stands your code doesn't compile. – Enigmativity May 29 '18 at 07:39
  • @Ask - And `MD5CryptoServiceProvider` is `IDisposable`. Don't forget to clean up after yourself. – Enigmativity May 29 '18 at 07:40
  • 2
    This feels like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). What is the purpose of all of this? – Corak May 29 '18 at 07:40
  • @Ask - And finally, when I fix your current code it seems to work as you want it to. What's wrong with the existing code? – Enigmativity May 29 '18 at 07:42
  • Actually I don't want to use regex. I just want to convert to base64 without special characters – Ask May 29 '18 at 07:46
  • 2
    @Ask - But these "special" characters are legal [Base64](https://en.wikipedia.org/wiki/Base64) characters. If you remove them, you have something that resembles but actually **isn't** base64. The question is, what is your "problem" with these characters? Do you need them in a URL? There is URL-Encode for that. – Corak May 29 '18 at 07:58
  • @Enigmativity, thanks for pointing out my mistake. I should use using (MD5 md5 = MD5.Create()) instead – Ask May 29 '18 at 08:48

0 Answers0