androidscrypt

Speeding up scrypt on android


I am using scrypt in making an Android app and it takes a very long time to compute the hash. This is how I call it:

String hash = Base64.encodeToString(SCrypt.scrypt("password".getBytes(), "salt".toString().getBytes(), 16384, 16, 2, 128), Base64.DEFAULT);

And this is how I declared the dependency in Gradle:

compile group: 'com.lambdaworks', name: 'scrypt', version: '1.4.0'

It takes almost a minute to compute the hash on my Nexus 6P and that is of course, very slow. Does anyone have any idea on how this can be made much faster? I am new to this and hence, clueless on why it is so slow and how to speed it up.


Solution

  • I think the SCrypt.scrypt()parameters should be optimized for your use cases.

    Some numbers in this answer and this slide p17

    (N = 2^14, r = 8, p = 1) for < 100ms (interactive use)

    (N = 2^20, r = 8, p = 1) for < 5s (sensitive storage)

    and the N,r,p meanings:

    N: General work factor, iteration count.

    r: blocksize in use for underlying hash; fine-tunes the relative memory-cost.

    p: parallelization factor; fine-tunes the relative cpu-cost

    So if you want less time, the N should be reduced. r and p is related to hardware, it need more runtime environment to adjust dynamically.