Hey folks, I am trying to implement a hmac sha256 ...
# kotlin-native
t
Hey folks, I am trying to implement a hmac sha256 function following this example (https://kotlinlang.slack.com/archives/C3SGXARS6/p1544768698174600?thread_ts=1541771174.265700&cid=C3SGXARS6 ), but that results in a
SIGABRT
when running it from xcode. Could anyone give me a hand on this? Thanks in advance.
o
why not follow example as is? cast of
ByteArray
to
CValuesRef<ByteVar>
doesn’t make sense, please use approach with pinned data
1
t
Thanks, I did that and made it work. Posting that here in case someone else needs it
Copy code
actual object Hmac256Encoder {
    @ExperimentalUnsignedTypes
    actual fun encode(key: ByteArray, value: String): ByteArray {
        val input = value.toUtf8()
        val digest = UByteArray(CC_SHA256_DIGEST_LENGTH)
        key.usePinned { keyPinned ->
            input.usePinned { inputPinned ->
                digest.usePinned { digestPinned ->
                    CCHmac(kCCHmacAlgSHA256, keyPinned.addressOf(0), key.size.convert(), inputPinned.addressOf(0), input.size.convert(), digestPinned.addressOf(0))
                }
            }
        }
        return digest.toByteArray()
    }
}
❤️ 1
m
Thanks for sharing @Tobi. This came up on a google search...
505 Views