Alexander Suslov
11/09/2018, 1:46 PMpackage = crypto
headers = CommonCrypto/CommonDigest.h
compilerOpts.ios = -I/opt/local/include
In my .kt file I have import crypto.*
line and I can use CC_SHA256 function.
But when I tried to migrate to K/N 1.3.0 I had a Unresolved reference: CC_SHA256
compile error.
I know that 1.3.0 already have a CommonCrypto.def
file, so I deleted my crypto.def
file and replaced import crypto.*
by import platform.CoreCrypto.*
.
Now I have no compile errors. But I have a linker error instead:
ld: warning: directory not found for option '-L/opt/local/lib'
ld: framework not found CommonCrypto
error: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/ld invocation reported errors
Could anybody help me?olonho
11/09/2018, 2:10 PMAlexander Suslov
11/09/2018, 2:13 PMimport kotlinx.cinterop.*
import platform.Foundation.*
import platform.CoreCrypto.*
actual class Sha256Encoder actual constructor() {
actual fun encode(text: String?): String {
if (text == null || text.isEmpty()) {
return "null"
}
memScoped {
val dataIn = (text as NSString).dataUsingEncoding(NSUTF8StringEncoding.toULong())!!
val macOut = NSMutableData.dataWithLength(CC_SHA256_DIGEST_LENGTH.convert())!!
CC_SHA256(dataIn.bytes as CValuesRef<ByteVar>, dataIn.length.toUInt(), macOut.mutableBytes as CValuesRef<UByteVar>);
val ba = macOut.bytes!!
val len = macOut.length.toInt()
return byteToHexString(ba.readBytes(len))
}
}
}
olonho
11/09/2018, 3:15 PM$HOME/.konan/kotlin-native-macos-1.0.1//klib/platform/macos_x64/CommonCrypto/manifest
line linkerOpts=-framework CommonCrypto
. We'll fix it in next release.olonho
11/09/2018, 3:22 PMAlexander Suslov
11/09/2018, 9:22 PM$HOME/.konan/kotlin-native-macos-1.0.1/klib/platform/ios_x64/CommonCrypto/manifest
and it works now. Thank you!nestserau
12/13/2018, 5:27 PMolonho
12/14/2018, 6:24 AMimport kotlinx.cinterop.*
import platform.CoreCrypto.CC_SHA256
import platform.CoreCrypto.CC_SHA256_DIGEST_LENGTH
fun main() {
val input = "Hi Kotlin".toUtf8()
val digest = UByteArray(CC_SHA256_DIGEST_LENGTH)
input.usePinned { inputPinned ->
digest.usePinned { digestPinned ->
CC_SHA256(inputPinned.addressOf(0), input.size.convert(), digestPinned.addressOf(0))
}
}
val digestString = digest.joinToString(separator = "") { it -> it.toString(16) }
println(digestString)
}
nestserau
12/14/2018, 9:42 AMTobi
04/17/2019, 9:55 AMval digestString = digest.joinToString("") { it.toString(16).padStart(2, '0') }