Warm tip: This article is reproduced from serverfault.com, please click

How can I convert a mail address to a hash in scala

发布于 2020-12-07 09:02:56

I want to take the hash of my mail addresses and write it to the log file.

logger.info(s"[RestService] Get permission request for $email")

this is my case but How can I get the hash of the email.

Helper or method?

Questioner
user14582396
Viewed
0
Felipe 2020-12-07 21:02:33

I suggest you to create an object and overwrite the hashCode method. Then use the scala.util.hashing.MurmurHash3 to create your hash code.

import scala.util.hashing.MurmurHash3

object TestMurmurHash {

  def main(args: Array[String]): Unit = {
    val email = MyObject("my_email@google.com")
    println(s"This is my email hash: $email and this is my hash: ${email.hashCode()}")

  }

  case class MyObject(val email: String) {

    override def equals(o: Any): Boolean = {
      this.hashCode() == o.hashCode()
    }

    override def hashCode(): Int = {
      MurmurHash3.stringHash(email)
    }

    def mailToHash(): Int = {
      MurmurHash3.stringHash(email)
    }
  }

}

output:

This is my email hash: MyObject(my_email@google.com) and this is my hash: -585889836