All Downloads are FREE. Search and download functionalities are using the official Maven repository.

secretblob.Generators.kt Maven / Gradle / Ivy

There is a newer version: 0.3.1
Show newest version
package se.wollan.crypto.secretblob

import se.wollan.crypto.toHexString
import se.wollan.crypto.xor
import java.nio.file.Path
import java.security.SecureRandom

// The actual file size will be double as we are using hex encoding of data to make it easier to handle.
private const val BLOB_SIZE = 65536

/** Use this to generate new random blob data, see unit test. */
fun generateNewSecretBlobData(): String {
    // gives 2048 keys with key-size 256 bits which should be enough for a while...
    val secretBlobData = ByteArray(BLOB_SIZE)
    SecureRandom().nextBytes(secretBlobData)
    return secretBlobData.toHexString()
}

/** Use this to generate a random hex mask for a new key, see unit test. */
fun generateRandomHexMaskForKey(key: SecretBlobKey): String {
    val secretBlobData = ByteArray(key.length)
    SecureRandom().nextBytes(secretBlobData)
    return secretBlobData.toHexString()
}

/** Use this to generate hex mask for encoding a specific string as a new key, see unit test. */
fun generateMaskToEncodeTextSecret(
    keyToUse: SecretBlobKey,
    allKeys: List,
    textSecret: String,
    secretBlobFilePath: Path
): String {
    val textBytes = textSecret.toByteArray(Charsets.UTF_8)
    require(textBytes.size == keyToUse.length) { "Change key length to ${textBytes.size}." }
    val secretBlob = SecretBlobModule(allKeys, secretBlobFilePath, enableCache = false).resolveSecretBlob()
    val secretKeyData = secretBlob.getSecretDataForKey(keyToUse.withMask("00".repeat(keyToUse.length)))
    val maskBytes = textBytes.xor(secretKeyData)
    return maskBytes.toHexString()
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy