-2

I am looking for something that acts sort of like a Hashing Algorithm, except I want it to generate an array of predefined length given any arbitrary string. As an example:

Arbitrary Seed: "Entropy"

Desired Length: 100 Reproducible characters from the seed.

Possible Output: EfvrlL9uGKolblPScba2ziPPON5QEm2Q0fsEPMURqu2NSVSXwaqkgCbjm0naeuoaATGWApJ1afBT3HxaWnoNuXCKSBY7EwCpTOga

The generated output does not in any way need to be cryptographically secure and the characters generated do not matter(They can be anything even unprintable characters). My only requirement is that the output be completely reproducible given the same seed. Does anyone have any suggestions on how I could go about achieving this?

Krythic
  • 4,184
  • 5
  • 26
  • 67
  • Does it have to be reversible? Do you want to be able to get the original string back? – Paul Hicks Aug 16 '16 at 00:38
  • 1
    `myString.GetHashCode().ToString().PadRight(100, '?')` – Paul Hicks Aug 16 '16 at 00:40
  • 1
    You also could keep appending hash results to a result while iterating through the original string and appending characters to the end. So the hash of "Entropy" + hash of "EntropyE" + hash of "EntropyEn" and so forth. Reproducible and arbitrary length without strings of repeated characters. – bitnine Aug 16 '16 at 00:43
  • @PaulHicks I tried your example, but it just generates a string with about 80ish '?' appended to it. – Krythic Aug 16 '16 at 00:45
  • 1
    That meets your requirements. – Paul Hicks Aug 16 '16 at 00:46
  • @PaulHicks Cheeky bastard. =P – Krythic Aug 16 '16 at 00:47
  • Not being cheeky. Just applying Occam's Razor. If you want the entire string to be gibberish, then you need a good reason: it adds no value above what my suggestion does. – Paul Hicks Aug 16 '16 at 00:47
  • @PaulHicks I'm just going to loop over the seed and xor it a few hundred different ways. Should suffice just fine. – Krythic Aug 16 '16 at 00:51
  • According to your requirements: why? That just increases the cost without increasing the value. Where is the benefit? What additional requirements do you have beyond "must be 100 characters" and "must be reproducible from the input"? – Paul Hicks Aug 16 '16 at 00:53
  • @PaulHicks Not be the same character. My initial example suggested this. Psuedo-random. – Krythic Aug 16 '16 at 00:57
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/121008/discussion-between-paul-hicks-and-krythic). – Paul Hicks Aug 16 '16 at 01:01
  • @PaulHicks I solved my problem. Thanks though. – Krythic Aug 16 '16 at 01:54
  • "sort of like a Hashing Algorithm, except I want it to generate an array of predefined length given any arbitrary string" <- isn't that exactly a hashing algorithm? – Blorgbeard Aug 16 '16 at 02:17

2 Answers2

0

I decided to go with my own pseudo-hash, which works pretty well. Here is the code I came up with; hopefully it can help someone else in the future:

        public static byte[] HashString(string seed, int length)
        {
            char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz".ToCharArray();
            byte[] hash = new byte[length];
            byte lastHash = 0;
            for (int i = 0; i < length; i++)
            {
                hash[i] = (byte)alphabet[(seed[(i % seed.Length)] ^ (i + lastHash)) % alphabet.Length];
                lastHash = hash[i];
            }
            return hash;
        }
Krythic
  • 4,184
  • 5
  • 26
  • 67
-1

This is a way you can do it, without creating your own hash:

var myString = "Entropy";
byte[] saltBytes = Encoding.ASCII.GetBytes("someSaltIWant");
var dBytes = new System.Security.Cryptography.Rfc2898DeriveBytes(myString, saltBytes).GetBytes(75);
var gibString = Convert.ToBase64String(dBytes);
Console.WriteLine(gibString);
// Always prints MVqAYJbmkxgQ4FdTD+a7/BlfZZLBVDXpsAAYtMuJ4aU5iejD+sB3tHqgSRoCg2KD1vnpI5eXhZa6vWvpOuM8dH8aOi1/zKMXuu4a

Even though you are not interested in security, I think with this hashing you can reach your desired 100 characters more easily. With MD5 or any SHA you will be short.

If you prefer a non-printable string, you can do this instead:

var dBytes = new System.Security.Cryptography.Rfc2898DeriveBytes(myString, strBytes).GetBytes(100);
string ugly = Encoding.ASCII.GetString(dBytes);
Andrew
  • 7,602
  • 2
  • 34
  • 42