I'm trying to encrypt string via sha256 in erlang, but I could not manage to get string back. crypto:hash(sha256, somestring) gives some binary, how can I get string?
The binary has to be decoded to an integer, and then printed in hexadecimal form:
1> io_lib:format("~64.16.0b", [binary:decode_unsigned(crypto:hash(sha256,
"somenewstring"))]).
"abf8a5e4f99c89cabb25b4bfde8a1db5478da09bcbf4f1d9cdf90b7b5321e43c"
binary:decode_unsigned/1 decodes the whole binary as one large big-endian unsigned integer. An alternative would be to pattern match the binary into an integer:
2> <<Integer:256>> = crypto:hash(sha256, "somenewstring").
<<171,248,165,228,249,156,137,202,187,37,180,191,222,138,
29,181,71,141,160,155,203,244,241,217,205,249,11,123,83,
...>>
3> Integer.
77784820141105809005227607825327585337759244421257967593474620236757179950140
4> io_lib:format("~64.16.0b", [Integer]).
"abf8a5e4f99c89cabb25b4bfde8a1db5478da09bcbf4f1d9cdf90b7b5321e43c"
(Note that <<Integer:256>> is equivalent to <<Integer:256/big-unsigned-integer>> since those are the default flags).
Alternative way how to get sha256 hashed string in Erlang:
[ element(C+1, {$0,$1,$2,$3,$4,$5,$6,$7,$8,$9,$A,$B,$C,$D,$E,$F}) || <<C:4>> <= crypto:hash(sha256,"somenewstring")]
Related
Is there a method to convert a binary string into a signed integer, for example:convert the string "11110000" into -16
I got NumberFormatException when trying to deal with the negative numbers. I used the following for the negative and positive numbers. You can try this one.
System.out.println(Integer.parseUnsignedInt("11111111111111111111111111110111", 2));
Output : -9
Problem: as title
What i got from web server to restore public key:
modulus: 9a91586b02a923d79302c5be83f25861452b78e59bd1d383045addc9debad1db9675726276a10f90bf0d0ae4880dbe4a54c821fffdb2f1394faf9df56d87408bb97398dcb2319fab7f53ee59fdb58def6f55ca91dbd9f2af65a4a36779f5353ec212d4bf99ba9197108acb2337d31d3efae038018dcb29665510641f32ac99e8152f297e2056ea14d9bd62350797b2da8edc23574326f57e1563952006dbbb133e2b15d2a4dd6a55aa7debfb28ba610d7e637022957b063e605985c402be41dd8dc3c3852645034f0b29f4fad0f45419f03f1bbb71c0dc54c1069a081d3edb73fd93a204edd0a99459b38e6486d41171328c5f53913696f8f2b1718019db9b65
exponent: 10001
What i have tried:
First base64encode the modulus and exponent then dump them into xml format, here's the result:
<RSAKeyValue><Modulus>OWE5MTU4NmIwMmE5MjNkNzkzMDJjNWJlODNmMjU4NjE0NTJiNzhlNTliZDFkMzgzMDQ1YWRk
YzlkZWJhZDFkYjk2NzU3MjYyNzZhMTBmOTBiZjBkMGFlNDg4MGRiZTRhNTRjODIxZmZmZGIy
ZjEzOTRmYWY5ZGY1NmQ4NzQwOGJiOTczOThkY2IyMzE5ZmFiN2Y1M2VlNTlmZGI1OGRlZjZm
NTVjYTkxZGJkOWYyYWY2NWE0YTM2Nzc5ZjUzNTNlYzIxMmQ0YmY5OWJhOTE5NzEwOGFjYjIz
MzdkMzFkM2VmYWUwMzgwMThkY2IyOTY2NTUxMDY0MWYzMmFjOTllODE1MmYyOTdlMjA1NmVh
MTRkOWJkNjIzNTA3OTdiMmRhOGVkYzIzNTc0MzI2ZjU3ZTE1NjM5NTIwMDZkYmJiMTMzZTJi
MTVkMmE0ZGQ2YTU1YWE3ZGViZmIyOGJhNjEwZDdlNjM3MDIyOTU3YjA2M2U2MDU5ODVjNDAy
YmU0MWRkOGRjM2MzODUyNjQ1MDM0ZjBiMjlmNGZhZDBmNDU0MTlmMDNmMWJiYjcxYzBkYzU0
YzEwNjlhMDgxZDNlZGI3M2ZkOTNhMjA0ZWRkMGE5OTQ1OWIzOGU2NDg2ZDQxMTcxMzI4YzVm
NTM5MTM2OTZmOGYyYjE3MTgwMTlkYjliNjU=</Modulus>
<Exponent>MTAwMDE=</Exponent>
</RSAKeyValue>
Then invoke RSACryptoServiceProvider.FromXmlString(publickey)
and error occurred:
Run-time error -2146893819(80090005)
Automation error
Bad data
Then i tried to generate xml format public key RSACryptoServiceProvider.ToXmlString(False)
and got
<RSAKeyValue><Modulus>ph0JbRrKHFY5sfmVa9cDPICAtYfT6OKF4KcjgBIKIuFRz3azyCCiE12qP0ZbuHqwb6YQxg6778NJK8S0Xvft6Fu9s0FCO7zUxVRaIw6gumOAV2ih/s+S9pFuxMf3k5w2v5iMA6TFjxS72kCa4O8iIXhOG4u05+o2fRC2cwEYVSk=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>
amazingly this key could be recognized by RSACryptoServiceProvider.FromXmlString
I didn't see much difference between the former one and the latter one, why does error occur in the first case?
Is there any other way to restore the public key given modulus and exponent?
You received a hex (base 16) version of the integer modulus and public exponent. You then converted that base 16 version into base64. However, the XML is supposed to contain the base64 encoding of the integers as byte arrays in big-endian format. You can easily do that from the hex by converting every two hex characters into its byte value. The result should look something like
<RSAKeyValue><Modulus>mpFYawKpI9eTAsW+g/JYYUUreOWb0dODBFrdyd660duWdXJidqEPkL8NCuSIDb5KVMgh//2y8TlPr531bYdAi7lzmNyyMZ+rf1PuWf21je9vVcqR29nyr2Wko2d59TU+whLUv5m6kZcQissjN9MdPvrgOAGNyylmVRBkHzKsmegVLyl+IFbqFNm9YjUHl7LajtwjV0Mm9X4VY5UgBtu7Ez4rFdKk3WpVqn3r+yi6YQ1+Y3AilXsGPmBZhcQCvkHdjcPDhSZFA08LKfT60PRUGfA/G7txwNxUwQaaCB0+23P9k6IE7dCplFmzjmSG1BFxMoxfU5E2lvjysXGAGdubZQ==</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>
I am using bro to read bytes directly of the payload of a packet.
I have a string value "\x10" and I want to get the decimal value of off that.
I know that bro support directly printing hex to decimal:
print 0x10;
Question is, how do I convert that string similarly to its integer version?
The best you can do is strip off the "\x" portion, and run it through 2 BIFs:
bytestring_to_count(hexstr_to_bytestring("10"));
I am trying to write a binary file which also has a string which i want to have as fixed length in vb.net. I tried lset, padleft in debug, the value returned is correct but in the output file, the first character before the string is the fixed length i specified. why does the binary writer write the additional char ?
I found out that if if you don't want or need the length byte you can call Write with a Char [] array instead of a String
When creating a password with sha1pass, the first value from the token, is '4'.
For instance:
sha1pass test
gives us:
$4$GTdnmykS$25iwV+ruXRwor4pUmKF57uXHj70$
The token uses $ as separators: 25iwV+ruXRwor4pUmKF57uXHj70 is the computed hash, GTdnmykS is the generated salt, since I didn't supply a second parameter, but what does that 4 mean?
The 4 is actually hardcoded, this is the last line of sha1pass, which is a Perl script:
print '$4$', $salt, '$', $pass, "\$\n";
Why is the first value of that token '4', and what does it mean?
It's just a signature so you know what you're looking at is a SHA-1 password; $1$ is used for MD5, $5$ for SHA-2-256, and $6$ for SHA-2-512.
I've seen the convention used where $1$ denotes MD5 and $4$ denotes SHA1. It's hardcoded because it's simply encoding how the hash was generated for future comparison.
It's a signature telling you the hash type is used, that much is obvious.
Now, why is is present? It's because it's used for unix password hashes, check crypt (Unix):
If the salt begins with the string $digit$ then the Modular Crypt Format is used. The digit represents which algorithm is used in encryption.