Improved secure cookie generation script
This commit is contained in:
parent
83ab962bda
commit
658b073ace
1 changed files with 33 additions and 12 deletions
|
|
@ -7,20 +7,41 @@ exec scala "$0" "$@"
|
|||
*/
|
||||
import java.security.{MessageDigest, SecureRandom}
|
||||
|
||||
lazy val random = SecureRandom.getInstance("SHA1PRNG")
|
||||
/**
|
||||
* @author <a href="http://jonasboner.com">Jonas Bonér</a>
|
||||
*/
|
||||
object Crypt {
|
||||
val hex = "0123456789ABCDEF"
|
||||
val lineSeparator = System.getProperty("line.separator")
|
||||
|
||||
val buffer = Array.fill(32)(0.byteValue)
|
||||
random.nextBytes(buffer)
|
||||
lazy val random = SecureRandom.getInstance("SHA1PRNG")
|
||||
|
||||
val digest = MessageDigest.getInstance("SHA1")
|
||||
digest.update(buffer)
|
||||
val bytes = digest.digest
|
||||
def md5(text: String): String = md5(unifyLineSeparator(text).getBytes("ASCII"))
|
||||
|
||||
val sb = new StringBuilder
|
||||
val hex = "0123456789ABCDEF"
|
||||
bytes.foreach { b =>
|
||||
val n = b.asInstanceOf[Int]
|
||||
sb.append(hex.charAt((n & 0xF) >> 4)).append(hex.charAt(n & 0xF))
|
||||
def md5(bytes: Array[Byte]): String = digest(bytes, MessageDigest.getInstance("MD5"))
|
||||
|
||||
def sha1(text: String): String = sha1(unifyLineSeparator(text).getBytes("ASCII"))
|
||||
|
||||
def sha1(bytes: Array[Byte]): String = digest(bytes, MessageDigest.getInstance("SHA1"))
|
||||
|
||||
def generateSecureCookie: String = {
|
||||
val bytes = Array.fill(32)(0.byteValue)
|
||||
random.nextBytes(bytes)
|
||||
sha1(bytes)
|
||||
}
|
||||
|
||||
def digest(bytes: Array[Byte], md: MessageDigest): String = {
|
||||
md.update(bytes)
|
||||
hexify(md.digest)
|
||||
}
|
||||
|
||||
def hexify(bytes: Array[Byte]): String = {
|
||||
val builder = new StringBuilder
|
||||
bytes.foreach { byte => builder.append(hex.charAt((byte & 0xF) >> 4)).append(hex.charAt(byte & 0xF)) }
|
||||
builder.toString
|
||||
}
|
||||
|
||||
private def unifyLineSeparator(text: String): String = text.replaceAll(lineSeparator, "\n")
|
||||
}
|
||||
|
||||
print("""
|
||||
|
|
@ -33,7 +54,7 @@ include "akka-reference.conf"
|
|||
akka {
|
||||
remote {
|
||||
secure-cookie = """")
|
||||
print(sb.toString)
|
||||
print(Crypt.generateSecureCookie)
|
||||
print(""""
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue