0

I am trying to port a python script that use scrypt to generate a password.

import pylibscrypt scrypt = pylibscrypt.scrypt password = 'test123'.encode("utf_16_be", "replace") salt = '384eaed91035763e'.decode('hex') key = scrypt(password, salt, 16384, 8, 1, 32) print (key.encode('hex')) 

Result should be: 9f2117416c7b2de9419a81c4625a28e40c16ac92aaa3000bb2c35844b3839eb1

In C, I use libsodium crypto_pwhash_scryptsalsa208sha256_ll() function.

If I try to pass the password from that variable:

const uint8_t password[] = "test123"; 

I am not getting the same key. I tried to remove ".encode("utf_16_be", "replace")" from the python script and then I get the same result.

So how can I convert my C variable to be the same as the python variable?

EDIT: The original software is in Java and seems to encode strings as UTF-16 big endian. Now I need to encode a C string to the same format.

14
  • Why not encode it as ASCII? Is there a chance the password will contain any non-ascii characters? Commented Feb 15, 2017 at 15:11
  • I think the python software must support unicode so this is why it is not ascii. Commented Feb 15, 2017 at 15:12
  • Since it is a password, you may restrict it to be ascii-only.. Commented Feb 15, 2017 at 15:13
  • I am converting a python script to C and the problem is that if I use ascii then it doesn't work since it give a different key. Commented Feb 15, 2017 at 15:14
  • Well.. on Linux you can use iconv and friends to convert between encodings. Commented Feb 15, 2017 at 15:17

1 Answer 1

0

I found a solution inspired from: Create UTF-16 string from char*

 size_t uint8_t_UTF_16(uint8_t *input, uint8_t *output, size_t inlen) { size_t outlen = 0.0; for (size_t i = 0; i < inlen; i++) { if (isascii(input[i])) { output[i] = 0; output[i + 1] = input[i / 2]; i++; outlen += 2; } else { output[i] = input[i]; outlen += 0.5; } } return outlen; } 

It does work but I'm not sure that this solution is safe.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.