MasterKey
public class MasterKey : KeyMaterial
A master key can be used to derive keys for other purposes.
Examples
let context = MasterKey.Context("Examples")!
let masterKey = MasterKey()
let subKey1 = masterKey.derive(sizeInBytes: MasterKey.DerivedKey.MinimumSizeInBytes, with: 0, and: context)!
let subKey2 = masterKey.derive(sizeInBytes: MasterKey.DerivedKey.MinimumSizeInBytes, with: 1, and: context)!
// You can also derive a key in order to use it with secret boxes
let secretBox = SecretBox(secretKey: masterKey.derive(with: 0, and: context))
-
The size of the master key in bytes.
Declaration
Swift
public static let SizeInBytes: UInt32
-
A context for which the derived keys should be used.
See moreDeclaration
Swift
public struct Context
-
Declaration
Swift
public class DerivedKey : KeyMaterial
-
Initialize a master key.
Declaration
Swift
public init()
-
Initialize a master key from a given byte array. he byte array is copied to a secure location and overwritten with zeroes to avoid the key being compromised in memory.
Warning
Do not initialize new keys with this function. If you need a new key, use
init?()
instead. This initializer is only to restore secret keys that were persisted.Declaration
Swift
public override init?(bytes: inout Bytes)
Parameters
bytes
A master key.
-
Derive a key with a given size for a given id and context.
A derived key will differ if the
id
or thecontext
differs.Declaration
Swift
public func derive(sizeInBytes: UInt32, with id: UInt64, and context: Context) -> DerivedKey?
Parameters
sizeInBytes
The size of the derived key in bytes.
id
The ID of the derived key.
context
A context in which the derived key is used.
-
Derive a secret key that can be used with
SecretBox
.Parameters
id
The ID of the derived key.]
context
A context in which the derived key is used.
Return Value
The secret key.
-
Derive a key that can be used for personalized hashing.
Declaration
Swift
public func derive(with id: UInt64, and context: Context, outputSizeInBytes: UInt32 = GenericHash.DefaultSizeInBytes) -> GenericHash.Key?
Parameters
id
The ID of the derived key.]
context
A context in which the derived key is used.
outputSizeInBytes
The size of the key in bytes.
Return Value
nil
if the size is not valid.