string encryption

Hiemlick Hiemlicker via Digitalmars-d digitalmars-d at puremagic.com
Fri Jul 1 18:31:17 PDT 2016


On Saturday, 2 July 2016 at 00:39:57 UTC, Basile B. wrote:
> On Saturday, 2 July 2016 at 00:05:14 UTC, Hiemlick Hiemlicker 
> wrote:
>> On Friday, 1 July 2016 at 23:55:08 UTC, Adam D. Ruppe wrote:
>>> On Friday, 1 July 2016 at 23:23:19 UTC, Hiemlick Hiemlicker 
>>> wrote:
>>
>> I've tried playing with opCall, opAssign, alias this, 
>> @property but writeln(s) never calls what I thought it should.
>>
>> I thought s was short for s() if s was a property. having 
>> alias this decrypt and decrypt being a @property should allow 
>> this to work?
>
> I think the best you can do is this:
>
> ========================
> import std.stdio;
>
> struct KryptedString(string value)
> {
>     alias get this;
>     string get() @property
>     {
>         return "decrypt \"" ~ value ~ "\" here";
>     }
> }
>
> template krypt(string value)
> {
>     string process()
>     {
>         return "crypted"; // encrypt the template param
>     }
>     enum krypt = KryptedString!process();
> }
>
> enum string1 = krypt!"blablabla";
> enum string2 = krypt!"blablablabla";
>
> void main()
> {
>     writeln(string1);
>     writeln(string2);
> }
> ========================
>
> The syntax is not so bad.
>
>    enum KryptedString string1 = "blablabla";
>
> is impossible or maybe I don't know the trick yet.

Wait! It almost works! ;)

But you are declaring string 1 and string 2 an enum. If you 
declare them as a string then the original is embedded in the 
binary!

I don't know why that would change anything but it does.

The reason it matter is because if one wanted to do a quick 
change of strings from normal to encrypted, they would also have 
to change all string variables to enums.




import std.stdio;

struct KryptedString(string value)
{
     alias get this;
     string get() @property
     {
		string q;
		foreach(c; value)
			q ~= c + 1;
		return q;
     }
}

template krypt(string value)
{
     string process()
     {
		string q;
		foreach(c; value)
			q ~= c - 1;
		return q;
     }
     enum krypt = KryptedString!process();
}

string string1 = krypt!"Testing 1 2 3 4";
string string2 = krypt!"ttttttaaaabbccd";

void main()
{
     writeln(string1);
     writeln(string2);
     getchar();
}

I guess one could do

enum string1e = krypt!"Testing 1 2 3 4";

then

string string1 = string1e;

but the goal is to make as clean as possible ;)




More information about the Digitalmars-d mailing list