imkcat / CatCrypto

An easy way for hashing and encryption.
MIT License
69 stars 13 forks source link

Argon2 hexStringValue seems incorrect #10

Closed zanechua closed 3 years ago

zanechua commented 3 years ago

Issue type

Environment

Description

Currently using CatCrypto for my downstream library, react-native-argon2 and this is a replacement for the SignalApp library as that has a GPL license and I would like to keep my package as MIT. Hence I chose your library. However I seem to be getting an incorrect hexadecimal value from the output of the Argon2 hashing. The string representation is correct however the hexadecimal value doesn't seem to be right.

Reproduce steps

Password: password Salt: 1234567891011121314151617181920212223242526272829303132333435363

Code:

    let argon2Context = CatArgon2Context.init();
    argon2Context.iterations = 2;
    argon2Context.memory = 32 * 1024;
    argon2Context.parallelism = 1;
    argon2Context.salt = salt;
    argon2Context.hashLength = 32;
    argon2Context.mode = .argon2id;

    let argon2Crypto = CatArgon2Crypto.init(context: argon2Context);
    let result = argon2Crypto.hash(password: password);

    if ((result.error) != nil) {
        let error = NSError(domain: "com.poowf.argon2", code: 200, userInfo: ["Error reason": "Failed to generate argon2 hash"])
        reject("E_ARGON2", "Failed to generate argon2 hash", error)
    }

    let rawHash = result.hexStringValue();
    let encodedHash = result.stringValue();

    let resultDictionary: NSDictionary = [
        "rawHash" : rawHash,
        "encodedHash" : encodedHash,
    ]

Expected: Hexadecimal value: 031d6c82ddede1200f4794605052745dd562bd4db358e23dac1b11c052eff8d9 String value: $argon2id$v=19$m=32768,t=2,p=1$MTIzNDU2Nzg5MTAxMTEyMTMxNDE1MTYxNzE4MTkyMDIxMjIyMzI0MjUyNjI3MjgyOTMwMzEzMjMzMzQzNTM2Mw$Ax1sgt3t4SAPR5RgUFJ0XdVivU2zWOI9rBsRwFLv+Nk

Received: Hexadecimal value: 246172676f6e32696424763d3139246d3d33323736382c743d322c703d31244d54497a4e4455324e7a67354d5441784d5445794d544d784e4445314d5459784e7a45344d546b794d4449784d6a49794d7a49304d6a55794e6a49334d6a67794f544d774d7a457a4d6a4d7a4d7a517a4e544d324d77244178317367743374345341505235526755464a30586456697655327a574f49397242735277464c762b4e6b00 String value: $argon2id$v=19$m=32768,t=2,p=1$MTIzNDU2Nzg5MTAxMTEyMTMxNDE1MTYxNzE4MTkyMDIxMjIyMzI0MjUyNjI3MjgyOTMwMzEzMjMzMzQzNTM2Mw$Ax1sgt3t4SAPR5RgUFJ0XdVivU2zWOI9rBsRwFLv+Nk

zanechua commented 3 years ago

Ah. I see where my issue was before. I am missing the configuration for hashResultType.

If I specify the hashResultType to be .hashRaw, I get the correct value for the hexadecimal representation but I now lose the string value.

I wonder if it's possible to have both representations without the need to run the hashing twice.

imkcat commented 3 years ago

Ah. I see where my issue was before. I am missing the configuration for hashResultType.

If I specify the hashResultType to be .hashRaw, I get the correct value for the hexadecimal representation but I now lose the string value.

I wonder if it's possible to have both representations without the need to run the hashing twice.

Thanks for using my library :)

If you read the source code, you'll find out the hash result is strongly depends on two functions: argon2d_hash_encoded & argon2d_hash_raw, It will call only one function in hashing time. It is unnecessary to hashing twice at the same time.

zanechua commented 3 years ago

Yep. I did read the source. I actually meant that if I use the CatCrypto library and I want to get both the encoded and raw hash at the same time, I would have to call the argon2 hasher twice. First to get the encoded, and second to get the raw hash. But I supposed this is default behaviour.