Skip to content

Commit dfc78cd

Browse files
authored
crypto_box: add SecretKey::as_bytes method (#180)
1 parent 20be967 commit dfc78cd

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

crypto_box/src/secret_key.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,32 @@ impl SecretKey {
5959
PublicKey(MontgomeryPoint::mul_base(&self.scalar))
6060
}
6161

62+
/// Borrows [`SecretKey`] as bytes.
63+
///
64+
/// # ⚠️Warning
65+
///
66+
/// The bytes are secret key material. Please treat them with
67+
/// the care they deserve!
68+
///
69+
/// # `Scalar` conversion notes
70+
///
71+
/// If you are using the `From<Scalar>` impl on [`SecretKey`] (as opposed
72+
/// to using [`SecretKey::from_bytes`] or one of the other methods that
73+
/// decodes a secret key from bytes), this method will return the same
74+
/// value as `Scalar::to_bytes`, which may reflect "clamping" if it was
75+
/// applied to the original `Scalar`.
76+
///
77+
/// In such cases, it may be undesirable to call this method, since such a
78+
/// value may not reflect the original scalar prior to clamping. We suggest
79+
/// you don't call this method when using `From<Scalar>` unless you know
80+
/// what you're doing.
81+
///
82+
/// Calling [`SecretKey::to_scalar`] can be used to safely round-trip the
83+
/// scalar value in such cases.
84+
pub fn as_bytes(&self) -> &[u8; KEY_SIZE] {
85+
&self.bytes
86+
}
87+
6288
/// Serialize [`SecretKey`] to bytes.
6389
///
6490
/// # ⚠️Warning

0 commit comments

Comments
 (0)