Groovy web console

subscribe to the feed Subscribe
to this
site

PasswordDigest

Published 1 month ago by Patricio Ascencio with tags criptography
Actions Execute script  ▶ Edit in console Back to console Show/hide line numbers View recent scripts
import java.util.UUID
import java.util.Date
import java.text.SimpleDateFormat
import java.util.Base64
import java.security.MessageDigest
import java.math.BigInteger


def buildCreatedDateTime(){
    dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'hh:mm:ss.SSSZ")
    return dateFormat.format(new Date());
}

def buildNonce(){
    uuid = UUID.randomUUID().toString()
}

def generatePasswordDigest(nonce,created,password){
    md = MessageDigest.getInstance("SHA-1");
    input = "${nonce}${created}${password}"
    messageDigest = md.digest(input.getBytes())
    return messageDigest

}

def generateBase64(passwordDigest){
    encoder = Base64.getEncoder()
    encodedBytes = encoder.encode(passwordDigest)
    return new String(encodedBytes)
}

nonce = buildNonce()
created = buildCreatedDateTime()
password = "DB65207D00F47465E35A565537CC0C04"

println(created)
println(nonce)
println(password)

passwordDigest = generatePasswordDigest(nonce,created,password)
println(passwordDigest)

passwordDigestEncoded = generateBase64(passwordDigest)

println(passwordDigestEncoded)