Generate and save to file a random string at compile time

hpf3

I want to insert a random string into a program code or a resource at compile time and save that value externally for later reference.

For detail:

I had an idea that it might be possible to identify individual builds of programs if you could randomly generate a string at compile time and insert it into both the program and a storage file such that if you were to copy an individual build between two computers it would share the same string, but if you compiled it again the "new" version would have a different string.

It just a thought I had and couldn't find anything for c#, just things specific to c/c++ (like templates), though i could see this being useful in cases where you had a program used by a group of employees and could give each one a separate build so if there was a leak it could be easier to trace.

peterpie

As far as I can see, your problem can be broken down into two parts

  1. Storing and retrieving the random string in your code
  2. Generating the random string at build-time

Storing and retrieving the random string in your code

This is the easy part. Because it will be auto-generated each time, you need to store it outside of your code. You can store it in a database table, a file, windows registry, on a remote server - it's up to you. That way, your code only needs a function to retrieve it from the location you saved it.

Generating the random string at build-time

I assume you are using visual studio, from the tag in the question. Here you can use the power of MSBuild. You would need to edit your CSPROJ file, to add the action to generate your random string during build and to push the generated string to the store location you chose above, inside an MSBuild targer.

The MSBuild targets you are interested in are either of these two:

<Target Name="BeforeBuild"><!--Runs before build begins-->
</Target>

And

<Target Name="AfterBuild"><!--Runs after build completes-->
</Target>

You can run literally anything within these targets. For an example helloworld using C#, take a look at the following link MSBuild Hello World on the Microsoft Documentation site.

Side note: I recommend the book Inside the Microsoft Build Engine - Using MSBuild and Team Foundation Build by Sayed Ibrahim Hashimi and William Bartholomew.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related