cryptography.Nonce.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of noise-kotlin Show documentation
Show all versions of noise-kotlin Show documentation
Noise protocols based on Diffie-Hellman key agreement
The newest version!
package nl.sanderdijkhuis.noise.cryptography
import nl.sanderdijkhuis.noise.data.Data
import nl.sanderdijkhuis.noise.data.Size
/** An incrementing but non-wrapping number to be used once for encryption. */
@JvmInline
value class Nonce(val value: ULong) {
val bytes: ByteArray get() = SIZE.byteArray { (value shr (it * Byte.SIZE_BITS)).toByte() }
fun increment(): Nonce? = if (value >= ULong.MAX_VALUE - 1uL) null else Nonce(value + 1uL)
companion object {
val SIZE = Size(8u)
val zero get() = Nonce(0uL)
fun from(data: Data): Nonce? =
if (data.size > SIZE) null
else Nonce(data.value.foldRight(0uL) { b, r -> (r shl Byte.SIZE_BITS) + b.toUByte() })
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy