We use essential cookies for the website to function, as well as analytics cookies for analyzing and creating statistics of the website performance. To agree to the use of analytics cookies, click "Accept All". You can manage your preferences at any time by clicking "Cookie Settings" on the footer. More Information.

Only Essential Cookies
Accept All
GuidesSystemSecurityCrypto Architecture KitKey DerivationKey Derivation Using Scrypt (ArkTS)

Key Derivation Using Scrypt (ArkTS)

Content overview

For details about the corresponding algorithm specifications, see Scrypt.

How to Develop

  1. Create an ScryptSpec object and use it as a parameter for key derivation.

    ScryptSpec is a child class of KdfSpec. You need to specify the following:

    • algName: algorithm to use, which is 'SCRYPT'.

    • passphrase: original password used to generate the derived key.

      If the string type is used, pass in the data used for key derivation instead of the string type such as HexString or base64. In addition, ensure that the string is encoded in UTF-8 format. Otherwise, the derived key may be different from what you expected.

    • salt: salt value.

    • n: number of iterations. The value must be a positive integer.

    • p: parallelization parameter. The value must be a positive integer.

    • r: block size. The value must be a positive integer.

    • maxMemory: maximum memory size. The value must be a positive integer.

    • keySize: length of the key to derive, in bytes. The value must be a positive integer.

  2. Call cryptoFramework.createKdf with the string parameter set to 'SCRYPT' to create a key derivation function object (Kdf) with the scrypt algorithm.

  3. Call Kdf.generateSecret with the SCRYPT object to generate a derived key.

    The following table lists how Kdf.generateSecret delivers the return value.

    Expand
    API Return Mode
    generateSecret(params: KdfSpec, callback: AsyncCallback<DataBlob>): void This API uses an asynchronous callback to return the result.
    generateSecret(params: KdfSpec): Promise<DataBlob> This API uses a promise to return the result.
    generateSecretSync(params: KdfSpec): DataBlob This API returns the result synchronously.
  • Return the result using await:

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import { cryptoFramework } from '@kit.CryptoArchitectureKit';
    2. import { BusinessError } from '@kit.BasicServicesKit';
    3. import { buffer } from '@kit.ArkTS';
    4. async function scryptAwait() {
    5. try {
    6. let spec: cryptoFramework.ScryptSpec = {
    7. algName: 'SCRYPT',
    8. salt: new Uint8Array(16),
    9. passphrase: 'password',
    10. n:1024,
    11. p:16,
    12. r:8,
    13. maxMemory:1024 * 16 * 8 * 10, //n * p * r * 10
    14. keySize: 64
    15. };
    16. let kdf = cryptoFramework.createKdf('SCRYPT');
    17. let secret = await kdf.generateSecret(spec);
    18. console.info('key derivation output: ' + secret.data);
    19. } catch(error) {
    20. let e: BusinessError = error as BusinessError;
    21. console.error('key derivation failed, errCode: ' + e.code + ', errMsg: ' + e.message);
    22. }
    23. }
  • Return the result using a promise:

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import { cryptoFramework } from '@kit.CryptoArchitectureKit';
    2. import { BusinessError } from '@kit.BasicServicesKit';
    3. import { buffer } from '@kit.ArkTS';
    4. function scryptPromise() {
    5. let spec: cryptoFramework.ScryptSpec = {
    6. algName: 'SCRYPT',
    7. passphrase: '123456',
    8. salt: new Uint8Array(16),
    9. n:1024,
    10. p:16,
    11. r:8,
    12. maxMemory:1024 * 16 * 8 * 10, //n * p * r * 10
    13. keySize: 64
    14. };
    15. let kdf = cryptoFramework.createKdf('SCRYPT');
    16. let kdfPromise = kdf.generateSecret(spec);
    17. kdfPromise.then((secret) => {
    18. console.info('key derivation output: ' + secret.data);
    19. }).catch((error: BusinessError) => {
    20. console.error(`key derivation failed: errCode: ${error.code}, message: ${error.message}`);
    21. });
    22. }
  • Return the result synchronously:

    Collapse
    Word wrap
    Dark theme
    Copy code
    1. import { cryptoFramework } from '@kit.CryptoArchitectureKit';
    2. import { BusinessError } from '@kit.BasicServicesKit';
    3. import { buffer } from '@kit.ArkTS';
    4. function kdfSync() {
    5. try {
    6. let spec: cryptoFramework.ScryptSpec = {
    7. algName: 'SCRYPT',
    8. passphrase: '123456',
    9. salt: new Uint8Array(16),
    10. n:1024,
    11. p:16,
    12. r:8,
    13. maxMemory:1024 * 16 * 8 * 10, //n * p * r * 10
    14. keySize: 64
    15. };
    16. let kdf = cryptoFramework.createKdf('SCRYPT');
    17. let secret = kdf.generateSecretSync(spec);
    18. console.info('[Sync]key derivation output: ' + secret.data);
    19. } catch(error) {
    20. let e: BusinessError = error as BusinessError;
    21. console.error('key derivation failed, errCode: ' + e.code + ', errMsg: ' + e.message);
    22. }
    23. }
Search in Guides
Enter a keyword.