简体   繁体   中英

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

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?

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

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