You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I noticed wyhash doesn't provide a way to convert a string or array of bytes into secret data. In most of the example code I've seen outside of this depot people use strings to seed the secret data poorly. There's no good examples.
At first I thought about XOR'ing the data with the _wyp default secret data. I just looped over the secret array until I ran out of data. Then I worried common letters in a string might cancel out each other. (For example, if both data[0] and data[32] were the same letter.) So I thought to mix the data in using _wymix:
But then I realized strings can be short and in those cases, this function may not perform as well as make_secret.
So I thought about making a special case for when data length is 8 bytes or less, but then it was getting to branchy for my taste. I thought, what if I don't use _wyp to initialize the secret data? What if I let make_secret initialize the secret data using the last 1 to 8 bytes?
This is what I've got currently:
staticvoidmake_secret_from_data(constvoid*data, int64_tlen, uint64_t*secret) {
constuint8_t*p= (constuint8_t*)data;
// Initialize secret with the last 1 to 8 bytesuint64_tremainder_len= ((len-1) % 8u) +1;
uint64_tremainder_start=len-remainder_len;
len-=remainder_len;
uint64_tremainder_data=0;
memcpy(&remainder_data, p+remainder_start, remainder_len);
remainder_data=_wyr8((constuint8_t*)&remainder_data); // Correct endianness?make_secret(remainder_data, secret);
// Mix in the rest of the datauint64_toffset=0;
while (len >= 8) {
secret[offset] =_wymix(*((uint64_t*)p), secret[offset]);
offset= (offset+1) % 4;
p+=8;
len-=8;
}
}
I had to do the weird ((len - 1) % 8u) + 1 thing so that when a len of 0 is passed in, it calls memcpy with 0 instead of 8.
Alternatively I thought about just using the last 8 bytes of data to seed make_secret regardless of alignment:
staticvoidmake_secret_from_data(constvoid*data, int64_tlen, uint64_t*secret) {
constuint8_t*p= (constuint8_t*)data;
// Initialize secret with the last 1 to 8 bytesuint64_tlast_len=len<8 ? len : 8;
uint64_tlast_start=len-last_len;
uint64_tlast=0;
memcpy(&last, p+last_start, last_len);
last=_wyr8((constuint8_t*)&last); // Correct endianness?make_secret(last, secret);
// Mix in the rest of the datauint64_toffset=0;
while (len>8) {
secret[offset] =_wymix(*((uint64_t*)p), secret[offset]);
offset= (offset+1) % 4;
p+=8;
len-=8;
}
}
The memcpy isn't always aligned in this case and the ternary operator adds an extra if branch, but those probably aren't a big deal overall. (I also adjusted the last loop from len >=8 to len > 8 since we can skip the last 8 now.)
Do either of those seem appropriate? Is there a better approach to building secret data from string/data?
I think it would be useful to have an official way to do this. Would you be interested in a pull request adding one of these make_secret_from_data functions? I would compress the code down to the style currently used.
The text was updated successfully, but these errors were encountered:
I noticed wyhash doesn't provide a way to convert a string or array of bytes into secret data. In most of the example code I've seen outside of this depot people use strings to seed the secret data poorly. There's no good examples.
At first I thought about XOR'ing the data with the
_wyp
default secret data. I just looped over the secret array until I ran out of data. Then I worried common letters in a string might cancel out each other. (For example, if bothdata[0]
anddata[32]
were the same letter.) So I thought to mix the data in using_wymix
:But then sometimes we have 1 to 7 bytes remaining.
But then I realized strings can be short and in those cases, this function may not perform as well as
make_secret
.So I thought about making a special case for when data length is 8 bytes or less, but then it was getting to branchy for my taste. I thought, what if I don't use
_wyp
to initialize the secret data? What if I letmake_secret
initialize the secret data using the last 1 to 8 bytes?This is what I've got currently:
I had to do the weird
((len - 1) % 8u) + 1
thing so that when alen
of 0 is passed in, it calls memcpy with 0 instead of 8.Alternatively I thought about just using the last 8 bytes of data to seed
make_secret
regardless of alignment:The memcpy isn't always aligned in this case and the ternary operator adds an extra
if
branch, but those probably aren't a big deal overall. (I also adjusted the last loop fromlen >=8
tolen > 8
since we can skip the last 8 now.)Do either of those seem appropriate? Is there a better approach to building secret data from string/data?
I think it would be useful to have an official way to do this. Would you be interested in a pull request adding one of these
make_secret_from_data
functions? I would compress the code down to the style currently used.The text was updated successfully, but these errors were encountered: