Home > Enterprise >  Generate Random Hexidecimal in Scala?
Generate Random Hexidecimal in Scala?

Time:05-25

How can I generate a random hexadecimal in scala?

The purpose it to essentially to use this as a UDF to generate random 64 char hexadecimals per column in a DataFrame.

I understand one can utilize below for an Int and etc:

val r = scala.util.Random
println(r.nextInt)

Is there an equivalent or another method simple method for a hexadecimal? particularly with 64 chars? Ex) 6e89f0c4c8a86812ef594229e5f4d997cb38aadc8a694f1b3be24a543b7699de

CodePudding user response:

Since a Byte is 2 hex digits, one can generate an array of 32 random bytes, render them as hex, and join them into a string:

def randomHex256(): String = {
  val arr = Array[Byte](32)
  scala.util.Random.nextBytes(arr)
  // iterator avoids creating a strict intermediate collection
  arr.iterator.map(b => String.format("x", Byte.box(b))).mkString("")
}

CodePudding user response:

The solution to this problem:As per @President James K. Polk , "64 hex chars is 32 bytes or equivalently 256 bits. Just generate 8 random ints or 4 random longs and print them in hex. See also formatting"

CodePudding user response:

I have a worked out sample available at this moment, the difference is as below: base64 has less overhead (base64 produces 4 characters for every 3 bytes of original data while hex produces 2 characters for every byte of original data).

import java.util.Base64

def encodeToBase64String(bytes: Array[Byte]): String = Base64.getEncoder.encodeToString(bytes)

val dm_with_clsr_two =(inputString:String) => encodeToBase64String(inputString.getBytes("UTF-8"))

spark.udf.register("DATA_MASK_TWO", dm_with_clsr_two)

spark.sql("select id,DATA_MASK_TWO(id), gender, birthdate, maiden_name, lname, fname, address, city, state, zip, cc_number, DATA_MASK_TWO(cc_number), cc_cvc, cc_expiredate from sample_ssn_data").show(5,false)

spark.close()

  • Related