Last active
April 12, 2022 10:13
-
-
Save serpro69/d98d1c1ddf88ee541d68d097799d54c3 to your computer and use it in GitHub Desktop.
tink/issues/69
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"primaryKeyId": 515944260, | |
"key": [{ | |
"keyData": { | |
"typeUrl": "type.googleapis.com/google.crypto.tink.AesGcmKey", | |
"keyMaterialType": "SYMMETRIC", | |
"value": "EgAaEDoFupL7XJF8sddiW94aDs8=" | |
}, | |
"outputPrefixType": "TINK", | |
"keyId": 515944260, | |
"status": "ENABLED" | |
}] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.crypto.tink.Aead | |
import com.google.crypto.tink.CleartextKeysetHandle | |
import com.google.crypto.tink.Config | |
import com.google.crypto.tink.JsonKeysetReader | |
import com.google.crypto.tink.JsonKeysetWriter | |
import com.google.crypto.tink.KeysetHandle | |
import com.google.crypto.tink.aead.AeadConfig | |
import com.google.crypto.tink.aead.AeadFactory | |
import com.google.crypto.tink.aead.AeadKeyTemplates | |
import java.io.File | |
import java.io.FileOutputStream | |
import java.nio.charset.Charset | |
import java.nio.file.Files | |
class Crypto(keySetFile: File) { | |
private val keySetHandle: KeysetHandle | |
private val aead: Aead | |
init { | |
Config.register(AeadConfig.TINK_1_0_0) | |
keySetHandle = readKeySetFile(keySetFile) | |
aead = AeadFactory.getPrimitive(this.keySetHandle) | |
} | |
companion object Writer { | |
fun writeNewKeySetFile(file: File) { | |
val keySetHandle = KeysetHandle.generateNew(AeadKeyTemplates.AES128_GCM) | |
CleartextKeysetHandle.write(keySetHandle, JsonKeysetWriter.withFile(file)) | |
} | |
} | |
private fun readKeySetFile(keySetFile: File): KeysetHandle { | |
return CleartextKeysetHandle.read(JsonKeysetReader.withFile(keySetFile)) | |
} | |
fun encryptFile(plaintext: ByteArray, outFile: File, associatedData: String) { | |
val ciphertext: ByteArray = aead.encrypt(plaintext, associatedData.toByteArray()) | |
FileOutputStream(outFile).use { it.write(ciphertext) } | |
} | |
fun decryptFile(file: File, associatedData: String): String { | |
val ciphertext: ByteArray = Files.readAllBytes(file.toPath()) | |
val plainText: ByteArray = aead.decrypt(ciphertext, associatedData.toByteArray()) | |
return plainText.toString(Charset.defaultCharset()) | |
} | |
} | |
fun main() { | |
val file = File("testfile") | |
val keysetFile = File("keyset.json") | |
val crypto = Crypto(keysetFile) | |
crypto.encryptFile("123".toByteArray(), file, "passphrase") | |
println(crypto.decryptFile(file, "passphrase")) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment