Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,894 questions

51,825 answers

573 users

How to generate SHA256 hash of a string in Scala

1 Answer

0 votes
import java.security.MessageDigest
import java.math.BigInteger

object SHA256Hash {
    def generateSHA256(s: String): String = {
        val digest = MessageDigest.getInstance("SHA-256")
        val hashBytes = digest.digest(s.getBytes("UTF-8"))
        val hashString = new BigInteger(1, hashBytes).toString(16)
        
        String.format("%64s", hashString).replace(' ', '0')
    }

    def main(args: Array[String]): Unit = {
        val s = "Scala Programming"
        
        val hash = generateSHA256(s)
        
        println(s"SHA256 hash of '$s' is: $hash")
    }
}

   
   
/*
run:

SHA256 hash of 'Scala Programming' is: f566b0d353dae18861c0277a6e1a03a671eba4455fda11cf025d1753520044f9
 
*/

 



answered Feb 6, 2025 by avibootz

Related questions

...