/
Codecs.scala
52 lines (44 loc) · 1.39 KB
/
Codecs.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* Copyright (C) 2009-2016 Typesafe Inc. <http://www.typesafe.com>
*/
package play.api.libs
import org.apache.commons.codec.digest.DigestUtils
import org.apache.commons.codec.binary.Hex
/**
* Utilities for Codecs operations.
*/
object Codecs {
/**
* Computes the SHA-1 digest for a byte array.
*
* @param bytes the data to hash
* @return the SHA-1 digest, encoded as a hex string
*/
def sha1(bytes: Array[Byte]): String = DigestUtils.sha1Hex(bytes)
/**
* Computes the MD5 digest for a byte array.
*
* @param bytes the data to hash
* @return the MD5 digest, encoded as a hex string
*/
def md5(bytes: Array[Byte]): String = DigestUtils.md5Hex(bytes)
/**
* Compute the SHA-1 digest for a `String`.
*
* @param text the text to hash
* @return the SHA-1 digest, encoded as a hex string
*/
def sha1(text: String): String = DigestUtils.sha1Hex(text)
/**
* Converts a byte array into an array of characters that denotes a hexadecimal representation.
*/
def toHex(array: Array[Byte]): Array[Char] = Hex.encodeHex(array)
/**
* Converts a byte array into a `String` that denotes a hexadecimal representation.
*/
def toHexString(array: Array[Byte]): String = Hex.encodeHexString(array)
/**
* Transform an hexadecimal String to a byte array.
*/
def hexStringToByte(hexString: String): Array[Byte] = Hex.decodeHex(hexString.toCharArray)
}