1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
import javafx.application.Application import javafx.scene.Scene import javafx.scene.control.Button import javafx.scene.control.TextArea import javafx.scene.layout.VBox import javafx.stage.FileChooser import javafx.stage.Stage import java.io.File import java.io.FileInputStream import java.security.MessageDigest import kotlin.experimental.and class ImageHashApp : Application() { private val textArea = TextArea() override fun start(primaryStage: Stage) { primaryStage.title = "Hash de Imagen" val selectImageButton = Button("Seleccionar Imagen") selectImageButton.setOnAction { val fileChooser = FileChooser() val selectedFile: File? = fileChooser.showOpenDialog(primaryStage) if (selectedFile != null) { val hash = calculateHash(selectedFile) textArea.text = "El hash de la imagen es:\n$hash" } } val vbox = VBox().apply { children.addAll(selectImageButton, textArea) } val scene = Scene(vbox, 300.0, 200.0) primaryStage.scene = scene primaryStage.show() } private fun calculateHash(file: File): String { val md = MessageDigest.getInstance("SHA-256") val fis = FileInputStream(file) val byteArray = ByteArray(8192) var bytesCount: Int while (fis.read(byteArray).also { bytesCount = it } != -1) { md.update(byteArray, 0, bytesCount) } fis.close() val bytes = md.digest() val sb = StringBuilder() for (i in bytes.indices) { sb.append(((bytes[i] and 0xff.toByte()) + 0x100).toString(16).substring(1)) } return sb.toString() } } fun main() { Application.launch(ImageHashApp::class.java) } |