Add js sample (node and browser)
This commit is contained in:
9 files changed
+198
-9
No files matched your search
@@ -0,0 +1,35 @@
|
||||
import com.ionspin.kotlin.crypto.hash.Hash
|
||||
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
|
||||
import com.ionspin.kotlin.crypto.util.toHexString
|
||||
import kotlinx.css.br
|
||||
import react.RBuilder
|
||||
import react.RComponent
|
||||
import react.RProps
|
||||
import react.RState
|
||||
import react.ReactElement
|
||||
import react.dom.h1
|
||||
|
||||
external interface RAppState : RState {
|
||||
var currentState: String
|
||||
}
|
||||
|
||||
external interface RAppProps : RProps {
|
||||
|
||||
}
|
||||
|
||||
class App(props: RAppProps) : RComponent<RAppProps, RAppState>(props) {
|
||||
override fun RBuilder.render() {
|
||||
val hash = Hash.sha512("123".encodeToUByteArray())
|
||||
h1 {
|
||||
+"Hash (SHA512) of 123: ${hash.toHexString()}"
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fun RBuilder.app(handler: RAppProps.() -> Unit): ReactElement {
|
||||
return child(App::class) {
|
||||
this.attrs(handler)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
|
||||
|
||||
import com.ionspin.kotlin.crypto.LibsodiumInitializer
|
||||
import com.ionspin.kotlin.crypto.hash.Hash
|
||||
import com.ionspin.kotlin.crypto.util.LibsodiumRandom
|
||||
import com.ionspin.kotlin.crypto.util.encodeToUByteArray
|
||||
import com.ionspin.kotlin.crypto.util.toHexString
|
||||
import react.dom.render
|
||||
import kotlinx.browser.document
|
||||
import kotlin.browser.window
|
||||
|
||||
fun main() {
|
||||
val runningOnNode = jsTypeOf(window) == "undefined"
|
||||
if (!runningOnNode) {
|
||||
LibsodiumInitializer.initializeWithCallback {
|
||||
render(document.getElementById("root")) {
|
||||
app {
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
LibsodiumInitializer.initializeWithCallback {
|
||||
val hash = Hash.sha512("123".encodeToUByteArray())
|
||||
println("Hash (SHA512) of 123: ${hash.toHexString()}")
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>Libsodium bindings sample app!</title>
|
||||
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
|
||||
</head>
|
||||
<body>
|
||||
<div id="root"></div>
|
||||
<script src="sample.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in new issue
Block a user