All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.leanplum.utils.HashUtil.kt Maven / Gradle / Ivy

The newest version!
package com.leanplum.utils

import java.security.MessageDigest

object HashUtil {

  private fun ByteArray.toHex(limit: Int): String =
    joinToString(separator = "", limit = limit, truncated = "") { eachByte ->
      "%02x".format(eachByte)
    }

  /**
   * Returns hexadecimal representation of sha256 on the input string.
   */
  fun sha256(text: String, limit: Int = 32): String {
    return MessageDigest
      .getInstance("SHA-256")
      .digest(text.toByteArray(Charsets.UTF_8))
      .toHex(limit)
  }

  /**
   * Get first 10 symbols from hexadecimal representation of sha256.
   */
  fun sha256_40(text: String) = sha256(text, 5)

  /**
   * Get first 50 symbols from hexadecimal representation of sha256.
   */
  fun sha256_200(text: String) = sha256(text, 25)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy