简体   繁体   中英

Generate Random Hexidecimal in Scala?

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

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("%02x", Byte.box(b))).mkString("")
}

Below is a sample code(Scala) for base64 where concept for the hex generation will be similar, the difference is as explained 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)

+-----------+--------------------------------+------+----------+-----------+------+--------+--------------------+-----------+-----+-----+-------------------+--------------------------------+------+-------------+
|id         |UDF:DATA_MASK_ONE(id)           |gender|birthdate |maiden_name|lname |fname   |address             |city       |state|zip  |cc_number          |UDF:DATA_MASK_TWO(cc_number)    |cc_cvc|cc_expiredate|
+-----------+--------------------------------+------+----------+-----------+------+--------+--------------------+-----------+-----+-----+-------------------+--------------------------------+------+-------------+
|2022-25-005|4DDA8A5D35947B12B948EFF6EF14579A|m     |1958/04/21|Smooth     |White |John    |10932 California Rd |Calfornia creek |CA|94025|5270 2020 2022 5516|4F88DDF6489891710B9C5A5D8412129E|123   |2010/06/25   |

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM