SHA-512 hascht ein Byte-Array in ColdFusion

Ich verwende ColdFusion 9

Unter Bezugnahme auf Ben Nadels gute Werkesein Blog, Ich habe es versucht

ucase(digestUtils.sha512(imageBinary))

Für SHA-512-Hashing bekomme ich das gefürchtet:

Die sha512-Methode wurde nicht gefunden. Entweder gibt es keine Methoden mit dem angegebenen Methodennamen und den angegebenen Argumenttypen, oder die sha512-Methode ist mit Argumenttypen überladen, die ColdFusion nicht zuverlässig entschlüsseln kann. ColdFusion hat 0 Methoden gefunden, die den angegebenen Argumenten entsprechen. Wenn dies ein Java-Objekt ist und Sie überprüft haben, dass die Methode vorhanden ist, verwenden Sie die Javacast-Funktion, um Mehrdeutigkeiten zu verringern.

Jetzt weiß ich, dass sha512 tatsächlich als Methode existiert, weil ich es gesehen habeHier, aber wenn ich eine

cfdump var="#digestUtils#"

Ich bekomme nur:

md5(byte[])     byte[]
md5(java.lang.String)   byte[]
md5Hex(byte[])  java.lang.String
md5Hex(java.lang.String)    java.lang.String
sha(java.lang.String)   byte[]
sha(byte[])     byte[]
shaHex(java.lang.String)    java.lang.String
shaHex(byte[])  java.lang.String

Was ist mit den anderen Methoden passiert? Ich muss wohl noch etwas anderes ausprobieren.

Bitte beraten Sie mit einer ColdFusion-Lösung. Eine ColdFusion / Java-Lösung wäre auch in Ordnung. Ich versuche, eine SSO-Anwendung zu schreiben, in der die Leute von Drittanbietern mir URL-Parameter zuführen. Ich habe den 1. Parameter erfolgreich dekodiert, um mein XML-Post zu erhalten. Ich muss jetzt den 2. Parameter nehmen, der die Hash-Nutzlast ist, und den Algorithmus durchgehen, um sicherzustellen, dass mein 1. Parameter nicht manipuliert wurde.

========= Hier beginnt die Bearbeitung: Okay, ich habe versucht, den Code erneut ohne Erfolg zu schreiben.

Der Algorithmus klingt einfach genug. Aber der Versuch, es umzusetzen, bringt mich um.

1. compute the hash string value of the XMLPost string above:
 a. convert the base64 salt string to a UTF-8 byte array.
 b. convert the base64 XML payload string to a UTF-8 byte array.
 c. create a new byte array consisting of the XML payload bytes from step b, appended with the salt bytes from step a.
 d. perform a SHA512 hash on the concatenated byte array from step c, which results in a hashed byte array.
 e. create a new byte array consisting of the hashed bytes from step d, appended with the salt bytes from step a.
 f. convert the result of step e to a base64-encoded string and should be the value of query string parameter "h" payload hash.

xmlPost wurde von meinen Drittanbietern als solche erstellt: Diese XML-Payload-Zeichenfolge wurde in ein UTF-8-Byte-Array konvertiert, das dann in eine Base-64-Zeichenfolge konvertiert wurde. Die resultierende Base-64-Zeichenfolge ist der Wert von xmlPost unten.

Also mache ich das:

<code>
<cfset xmlPost = urlDecode("PD94bWwgdmVyc2lvbj0iMS4wIj8%2bPEVzdG9yZVNzb0N1c3RvbWVyIHhtbG5zOnhzaT0iaHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxTY2hlbWEtaW5zdGFuY2UiIHhtbG5zOnhzZD0iaHR0cDovL3d3dy53My5vcmcvMjAwMS9YTUxTY2hlbWEiPjxDdXN0b21lcklkPjExMjk0MDwvQ3VzdG9tZXJJZD48RGVhbGVyQ29kZT5OODg4ODg8L0RlYWxlckNvZGU%2bPFBvaW50QmFsYW5jZT4yODA8L1BvaW50QmFsYW5jZT48Rmlyc3ROYW1lPkZhaXRoPC9GaXJzdE5hbWU%2bPExhc3ROYW1lPkh1dHVsYTwvTGFzdE5hbWU%2bPC9Fc3RvcmVTc29DdXN0b21lcj4%3d") />
<cfset salt = "3dfjh674!MujErf98344@090" />
<cfset payload_hash = urlDecode("EtLDRJfcRESFKpY4OGZZnRSN2THqT%2bEelzOuXVU06jotd2kE4yKnlYay7BqyAdcUSATRgSMaHxZa6uBqKKd9rjNkZmpoNjc0IU11akVyZjk4MzQ0QDA5MA%3d%3d") />

<cfset strXML = ToString( ToBinary( xmlpost ) ) /> <!--- to get actual XML --->

<!--- base64 encoding returns a byte array --->
<cfset saltByteArray = toBase64( salt, "utf-8" )  /> 
<cfset xmlpostByteArray = toBase64( xmlPost, "utf-8" ) />
<!--- append salt to xmlpost --->
<cfset xmlpostsaltByteArray = xmlpostByteArray & saltByteArray />

<!--- now let us perform a sha512 hash on this concatenated byte array --->
<cfscript>
// Create an instance of our DigestUtils class
digestUtils = createObject("java","org.apache.commons.codec.digest.DigestUtils");
// I hash a byte array using the given algorithm and return a
// 32-character Hexadecimal string. Home-made hash function for CF9 and earlier
function hashBytes( bytes, algorithm = "SHA-512" ){
    // Get our instance of the digest algorithm that we'll use
    // to hash the byte array.
    var messageDigest = createObject( "java", "java.security.MessageDigest" ).getInstance( javaCast( "string", algorithm ) );

    // Get the digest for the given byte array. This returns the
    // digest (i.e., hash) in byte-array format.
    var digest = messageDigest.digest( bytes );

    // Now that we have our digested byte array (i.e., our hash as another byte
    // array), we have to convert that into a HEX string. So, we'll need a HEX buffer.
    var hexBuffer = [];

    // Each integer in the byte digest needs to be converted into
    // a HEX character (with possible leading zero).
    for (byte =1 ;byte LTE ArrayLen(digest);byte = byte + 1) {
    //for ( var byte in digest){
        // Get the hex value for this byte. When converting the
        // byte, only use the right-most 8 bits (last 8 bits of the integer)
        // otherwise the sign of the byte can create oddities

        var tail = bitAnd( 255, byte );

        // Get the hex-encoding of the byte.
        var hex = ucase( formatBaseN( tail, 16 ) );

        // In order to make sure that all of the HEX characters
        // are two-digits, we have to prepend a zero for any
        // value that was originally LTE to 16 (the largest value
        // that won't result in two HEX characters).
        arrayAppend( hexBuffer, (tail <= 16 ? ("0" & hex) : hex) );
    }

    // Return the flattened character buffer.
    return( arrayToList( hexBuffer, "" ) );
}

// Get the hash of the byte array using our hashBytes() function
hashByteArray = hashBytes( xmlpostsaltByteArray );  
</cfscript>


<!--- The hashByteArray is in HEX format now. Convert to binary --->
<!--- You must binary decode the hashed string before converting it to binary --->
<cfset hashByteArray = toBase64( BinaryDecode( hashByteArray, 'HEX' ) ) />

<!--- The final step is to append this new hashbytearray with the salt byte array --->

<cfset hashByteArray = hashByteArray & saltByteArray />

<!--- now convert this value to a base64 encoded string --->

<cfset hashByteArray2 = toBase64( hashByteArray )/>

Folgendes bekomme ich für meine strXML-Variable:

Actual xml structure converted from base 64 to string:
<?xml version="1.0"?><EstoreSsoCustomer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><CustomerId>112940</CustomerId><DealerCode>N88888</DealerCode><PointBalance>280</PointBalance><FirstName>Faith</FirstName><LastName>Hutula</LastName></EstoreSsoCustomer>  

Der endgültige Wert hasByteArray2 ähnelt nicht einmal payload_hash

Dies ist mein erstes Mal und mein Verständnis von Hashing, Bytearrays und Zeichenkonvertierungen ist vor Jahrzehnten aus dem Fenster geflogen.

Was mache ich falsch?

Vielen Dank, dass Sie Faith Sloan

Antworten auf die Frage(1)

Ihre Antwort auf die Frage