Standard way to hash an RSA key? - encryption

What's the algorithm for creating hash (sha-1 or MD5) of an RSA public key? Is there a standard way to do this? Hash just the modulus, string addition of both and then take a hash? Is SHA-1 or MD5 usually used?
I want to use it to ensure that I got the right key (have the sender send a hash, and I calculate it myself), and log said hash so I always know which exact key I used when I encrypt the payload.

Based on the OpenSSH source code, the way that a fingerprint is generated for RSA keys is to convert n and e from the public key to big-endian binary data, concatenate the data and then hash that data with the given hash function.
Portions of the OpenSSH source code follows. The comments were added to clarify what is happening.
// from key_fingerprint_raw() in key.c
switch (k->type) {
case KEY_RSA1:
// figure out how long n and e will be in binary form
nlen = BN_num_bytes(k->rsa->n);
elen = BN_num_bytes(k->rsa->e);
len = nlen + elen;
// allocate space for n and e and copy the binary data into blob
blob = xmalloc(len);
BN_bn2bin(k->rsa->n, blob);
BN_bn2bin(k->rsa->e, blob + nlen);
...
// pick a digest to use
switch (dgst_type) {
case SSH_FP_MD5:
md = EVP_md5();
break;
case SSH_FP_SHA1:
md = EVP_sha1();
break;
...
// hash the data in blob (n and e)
EVP_DigestInit(&ctx, md);
EVP_DigestUpdate(&ctx, blob, len);
EVP_DigestFinal(&ctx, retval, dgst_raw_length);
From the BN_bn2bin manual page:
BN_bn2bin(a, to) converts the absolute value of a into big-endian form and stores it at to. to must point to BN_num_bytes(a) bytes of memory.

Related

encryption decryption

I am trying to create a custom encryption/decryption code in python. I read few articles and found that I can store the encryption ciphers in the dictionary. I tried using it but it is not working. keys will be letters in alphabetical order(A-Z) and values will be the letters that I want to put(ciphers). There is no specifics shift pattern in the encryption cipher. There are also numbers(0-9) in the encryption-decryption. they are in sequence though.
dict = {'A' : 'O', 'B' : 'P', 'C' : 'J' ......'Z' : 'Z', ....
'0' : 9, '1' : 8, ............'8' : 1, '9' : 0}
These encrypted password will be stored in a database. I need help to create the code to encrypt and decrypt the password. If there's any other method to start with please let me know.
If you don't want to use the Python Cryptography Module...
Here's some code that may help:
I'm replacing your dict with the name en_dict
#unencrypted_item should be a string
def encrypt(unencrypted_item):
result = ""
for i in unencrypted_item:
result+=en_dict[i]
return result
def encrypt(encrypted_item):
result = ""
for i in encrypted_item:
for n in en_dict:
if en_dict[n] == i:
result += n
break
return result
Try mapping the original string with the keys in your dictionary.
mapping = {'A':'Z', 'B':'C', '1':'0', '2':'7'}
reverse_mapping = dict((j,i) for i,j in mapping.items())
string_to_encrypt = 'AB1'
encrypted_string = ''.join(map(mapping.__getitem__, string_to_encrypt))
print(encrypted_string) #ZC0
decrypted_string = ''.join(map(reverse_mapping.__getitem__, encrypted_string))
print(decrypted_string) #AB1
Firstly, I hope that you plan to use your encryption scheme for academic purposes only because this simple substitution cipher is very insecure. For the most basic level of security you should look at storing salted hashes of user passwords instead, or use something like bcrypt.
WRT to your specific question, use your mapping dictionary with str.translate() to translate the string in one go without the need to explicitly loop in your code, or use other functions such as map() and join().
encrypt_dict = {'A': 'O', 'B': 'P', 'C': 'J', 'Z': 'Z', '0': 9, '1': 8, '8': 1, '9': 0}
tr_encrypt = {ord(str(k)): ord(str(encrypt_dict[k])) for k in encrypt_dict}
tr_decrypt = {tr_encrypt[k]: k for k in tr_encrypt}
plaintext = 'ABCZ0189'
ciphertext = plaintext.translate(tr_encrypt)
>>> ciphertext
'OPJZ9810'
assert ciphertext.translate(tr_decrypt) == plaintext
plaintext = 'THIS IS test 128'
ciphertext = plaintext.translate(tr_encrypt)
>>> ciphertext
'THIS IS test 821'
assert ciphertext.translate(tr_decrypt) == plaintext
The first few lines set up encryption and decryption translation tables in the form required by str.translate(). Then it's simply a matter of calling translate on the string to encrypt and decrypt as required.
The second example shows that any character in the string that is not present in the translation table is left unchanged which may or may not be what you want. You can create a translation mapping that covers all expected characters, but that's potentially a lot if you are using unicode.
I think this is what you want. You can use string.printable[:61] where string.printable prints all the ASCII characters and [:61] takes all characters from index 0 to 61 - which are letters and numbers
import string
import random
normla_l=list(string.printable[:61])
new_l=list(string.printable[:61])
random.shuffle(new_l)
see_list={x:y for x,y in zip(normla_l,new_l)}
def change_list(char):
return see_list.get(char) if char!='' else random.choice(normla_l)
x=input("Enter string: ")
print(''.join([change_list(char.strip()) for char in x]).strip())
Sample
Enter string: This string will be encrypted
Xqv7G74HvTxbPv55psIVITzHhg4IN

How does bcrypt "know" whether a given hash is associated with a given password?

Given a password P and hash H, the function bcrypt.compare(P, H) tells you whether or not H is a bcrypt hash of P.
Question: How does bcrypt.compare do the above? It's mysterious to me since P may be associated with many different hashes, and bcrypt itself doesn't seem to have any "memory" of the hashes it creates for P.
(Bonus question: Am I right to assume that the above implies that each bcrypt hash is associated with exactly one password? Or am I wrong -- may a hash be associated with many passwords?)
Hashing:
string BCryptHashPassword(password)
{
Byte[] salt = GenerateSomeSalt();
return DoTheHash(password, salt);
}
Verifying:
Boolean BCryptVerifyPassword(password, expectedHash)
{
Byte[] salt = ExtractSaltFromExpectedHash(expectedHash);
String actualHash = DoTheHash(password, salt);
return (actualHash == expectedHash);
}
It hashes the password again with the original salt, and compares them.
}
BCrypt does not only save the hash in H it also saves among other things the salt used to create the hash (here a full explanation).
Match takes the raw password and the expected hash to match a password, so it simply extracts the salt from the expected hash and reapplies it to the raw password.

Hash collisions for golang built-in map and string keys?

I wrote this function to generate random unique id's for my test cases:
func uuid(t *testing.T) string {
uidCounterLock.Lock()
defer uidCounterLock.Unlock()
uidCounter++
//return "[" + t.Name() + "|" + strconv.FormatInt(uidCounter, 10) + "]"
return "[" + t.Name() + "|" + string(uidCounter) + "]"
}
var uidCounter int64 = 1
var uidCounterLock sync.Mutex
In order to test it, I generate a bunch of values from it in different goroutines, send them to the main thread, which puts the result in a map[string]int by doing map[v] = map[v] + 1. There is no concurrent access to this map, it's private to the main thread.
var seen = make(map[string]int)
for v := range ch {
seen[v] = seen[v] + 1
if count := seen[v]; count > 1 {
fmt.Printf("Generated the same uuid %d times: %#v\n", count, v)
}
}
When I just cast the uidCounter to a string, I get a ton of collisions on a single key. When I use strconv.FormatInt, I get no collisions at all.
When I say a ton, I mean I just got 1115919 collisions for the value [TestUuidIsUnique|�] out of 2227980 generated values, i.e. 50% of the values collide on the same key. The values are not equal. I do always get the same number of collisions for the same source code, so at least it's somewhat deterministic, i.e. probably not related to race conditions.
I'm not surprised integer overflow in a rune would be an issue, but I'm nowhere near 2^31, and that wouldn't explain why the map thinks 50% of the values have the same key. Also, I wouldn't expect a hash collision to impact correctness, just performance, since I can iterate over the keys in a map, so the values are stored there somewhere.
In the output, all runes printed are 0xEFBFBD. It's the same number of bits as the highest valid unicode code point, but that doesn't really match either.
Generated the same uuid 2 times: "[TestUuidIsUnique|�]"
Generated the same uuid 3 times: "[TestUuidIsUnique|�]"
Generated the same uuid 4 times: "[TestUuidIsUnique|�]"
Generated the same uuid 5 times: "[TestUuidIsUnique|�]"
...
Generated the same uuid 2047 times: "[TestUuidIsUnique|�]"
Generated the same uuid 2048 times: "[TestUuidIsUnique|�]"
Generated the same uuid 2049 times: "[TestUuidIsUnique|�]"
...
What's going on here? Did the go authors assume that hash(a) == hash(b) implies a == b for strings? Or am I just missing something silly? go test -race isn't complaining either.
I'm on macOS 10.13.2, and go version go1.9.2 darwin/amd64.
String conversion of an invalid rune returns a string containing the unicode replacement character: "�".
Use the strconv package to convert an integer to text.

What kind of hash encryption is this?

Im trying to find out what kind of hash encryption this is in javascipt:
function L(j) {
var h = a.pbkey,
g = a.rndnb;
if (typeof f != "undefined") {
var e = f.PBK(h),
d = f.STBUNC(j),
i = f.STBA(g),
c = f.ENC(d, e, i),
b = f.BAT64(c);
return b
} else return ""
}
It hash a phonenumber.Ex: +79645531974
pbkey:'e=010001;m=d06889ab9ea3c9ce9d2e92559d6b2f043ef873f0cc9f858745bc6784d1d9a98f8d354e061d25fad9c3a741c57626d0d65de01eb03584a5af1f579b9f9834a60b628756ad636ec1d3129e87e0c7274670d9ca615f12fe3424e9da362f7f8cce8dfe61d79f5ec68dffe1f3ddcf5e20e1bf07349ee6c747a59b09d6420d131899f93dfaeacd76d6a684cda8ac99e7f87e17507235a2b59b84f56d8a3d4ecdd8259b3c7d892758d69a48930b591e66cc6d88d20a3de9360be30c8a94084a2753c92e0cfd1c94868c90109cd176855bba96cd3e73f34442ddfd256da7f1d1e48fbf265ad2f2caeebe4990ca5638b90b6c3fafa8c015a09947e3f03defc51e231a2f2d'
rndnb:'7fc1cdfea47d0057bbb33176ce73a376f9319d4e221d84807d74ff2f859b510b9fd132e577ed207d96b1d11e57500bff93efe97842248bcbe39527592797b7e3a821110ae61c3da67c2773bcb634c53357fb230ef95297d20c37d256aa8bd75bea315f2d
Result: 4LW/+zyiBBgDExOLPLafO9T/GG3guycSMK3uz16qFcXWgvo1KAF8VrbGrxAE91Mvk6qUDkX8c9ha7urDB41XDAhciBbj2VzE48WXjB/A6gI6n7qcTwkNTPT0Qly1EFRtTF44xTbPEld/OviYhD2OolumbtL42wtnyw1oh4/2v2SyAqARdGJizRhd1UFpWW+OUIcF3eyhKS1R+TDorsOoM/bJQzR6CTSyLysfPJL8ldjG0Ujevac7dT+WvaXFmP3qlsReMP/FSLjs7xixCAA/VrxIRUragoIOf2cptilop5zJNY26DO/iEhUUU7n8ANayrqthplS3v624XR24iM22bg==
The code suggests that it uses 2048 bit RSA encryption (with public exponent 65537) and a randomized padding scheme.
But with only this code it is impossible to solve the whole thing. We need to know what the PBK(), STBUNC(), STBA(), ENC(), BAT64() functions do, otherwise we cannot definitively say anything about what is done here.
However, the input parameters give some suggestions. The pbkey parameter suggests that the encryption is "public-key" based and uses an exponent e = 65537 (a commonly used RSA public exponent). Then it's an easy guess that m stands for "modulus". We could be dealing with either RSA or some logarithm-group crypto (e.g. ElGamal). To get more information we could check if this modulus m is a prime number, turns out it's not. So logarithm-group crypto is off the table and so we are probably dealing with RSA. Notice how the modulus and the output are each 2048 bits, so this checks out.
But we're not done. We also have the rndnb value, which probably means "random number". This suggests that the encryption is not textbook RSA (thank god), but uses some kind of randomization. Possible it uses some kind of standard padding scheme like OAEP.
To get more details we would really need to get the bodies of the functions that are use.

how to design/create key for key/value storage?

I want to store serialized objects (or whatever) in a key/value cache.
Now I do something like this :
public string getValue(int param1, string param2, etc )
{
string key = param1+"_"+param2+"_"+etc;
string tmp = getFromCache();
if (tmp == null)
{
tmp = getFromAnotherPlace();
addToCache( key, tmp);
}
return tmp;
}
I think it can be awkward. How can I design the key?
if i understood the question, i think the simplest and smartest way to make a key is to use an unidirectional hash function as MD5, SHA1 ecc...
At least two reason for doing this:
The resulting key is unique for sure!(actually both MD5 and SHA1 have been cracked (= )
The resulting key has a fixed lenght!
You have to give your object as argument of the function and you have your unique key.
I don t know very much c# but i am quite sure you can find an unidirectional hash function builted-in.
First of all your key seems to be composed out of a lot of characters. Keep in mind that the key name also occupies memory (1byte / char) so try to keep it as short as possible. I've seen situations where the key name was larger than the value, which can happen if you have cases where you store an empty array or an empty value.
The key structure. I guess from your example that the object you want to store is identified by the params (one being the item id maybe, or maybe filters for a search [...]). Start with a prefix. The prefix should be the name of the object class (or a simplified name depicting the object in general).
Most of the time, keys will have a prefix + identifier. In your example you have multiple identifiers. If one of them is a unique id, go with only prefix + id and it should be enough.
If the object is large and you don't always use all of it then change your strategy to a multiple key storage. Use one main key for storing the most common values, or for storing the components of the object, values of which are stored in separate keys. Make use of pipes and get the whole object in one connection using one "multiple" query :
mainKey = prefix + objectId;
object = getFromCache(mainKey);
startCachePipeline();
foreach (object[properties] as property) {
object->property = getFromCache(prefix + objectId + property);
}
endCachePipeline();
The structure for an example "Person" object would then be something like :
person_33 = array(
properties => array(age, height, weight)
);
person_33_age = 28;
person_33_height = 6;
person_33_weight = 150;
Memcached uses memory most efficient when objects stored inside are of similar sizes. The bigger the size difference between objects (not talking about 1 lost big object or singular cases, although memory gets wasted then as well) the more wasted memory.
Hope it helps!

Resources