Skip to content

Encryption

getRandomValues

TIP

The API usage is as follows: wx.getRandomValues(Object object)

  • Functional description: Get cryptographically secure random numbers

  • Parameters and descriptions: Object object。

    PropertiesTypeDefault valueRequiredDescription
    lengthnumber-YesInteger, the number of bytes to generate random numbers, maximum 1048584
    successFunction-NoCallback function for successful interface call
    failFunction-NoCallback function for failed interface call
    completeFunction-NoCallback function for interface call completion (executed regardless of success or failure)
  • object.success callback function parameters: Object res。

    PropertiesTypeDescription
    randomValuesArrayBufferRandom number content, length is the number of bytes passed in
  • Sample code:

js
wx.getRandomValues({
  length: 6 // Generate a random number of 6-byte length,
  success: res => {
    console.log(wx.arrayBufferToBase64(res.randomValues)) // Print after converting to a base64 string
  }
})