Skip to content

Commit 06fe46a

Browse files
authored
Docs [Cryptographic Helper Function] (#191)
- [+] refactor(otpverifier): improve documentation for cryptoPow10n function - [+] Key points: - [+] The function calculates the value of 10 raised to the power of n (10ⁿ). - [+] It uses recursive multiplication to compute the result. - [+] The base case is when n ≤ 0, where the result is 1 (10⁰ = 1). - [+] For n > 0, the function recursively multiplies 10 with the result of cryptoPow10n(n-1). - [+] The function assumes that n is non-negative and returns 1 for negative values of n. - [+] The purpose of the function is to calculate the appropriate modulo value based on the desired number of digits for HOTP and TOTP values. - [+] The function is used in the truncation step of the HOTP and TOTP algorithms to ensure the resulting values have the specified number of digits. - [+] The documentation includes a "Magic Calculator" section that explains the mathematical calculations performed by the function.
1 parent 4fb2ff7 commit 06fe46a

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

internal/otpverifier/otpverifier.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -508,27 +508,34 @@ func (v *Config) DecodeBase32WithPadding() []byte {
508508
return bytes
509509
}
510510

511-
// cryptoPow10n calculates the value of 10 raised to the power of n.
511+
// cryptoPow10n calculates the value of 10 raised to the power of n (10ⁿ).
512512
//
513513
// The function uses recursive multiplication to compute the result.
514-
// It starts with the base case of n <= 0, where the result is 1 (10^0 = 1).
514+
// It starts with the base case of n 0, where the result is 1 (10 = 1).
515515
// For n > 0, the function recursively multiplies 10 with the result of cryptoPow10n(n-1).
516516
//
517517
// Example:
518518
//
519519
// v.cryptoPow10n(0) = 1
520520
// v.cryptoPow10n(1) = 10
521-
// v.cryptoPow10n(2) = 10 * v.cryptoPow10n(1) = 10 * 10 = 100
522-
// v.cryptoPow10n(3) = 10 * v.cryptoPow10n(2) = 10 * 100 = 1000
521+
// v.cryptoPow10n(2) = 10 × v.cryptoPow10n(1) = 10 × 10 = 10²
522+
// v.cryptoPow10n(3) = 10 × v.cryptoPow10n(2) = 10 × 10² = 10³
523523
//
524524
// The function returns the calculated value as an unsigned 32-bit integer [uint32].
525525
//
526-
// Note: The function assumes that n is non-negative. If n is negative, it will result
527-
// in infinite recursion and may cause a stack overflow.
526+
// Note: The function assumes that n is non-negative. If n is negative, it will return 1 (10⁰ = 1).
528527
//
529528
// The purpose of this function is to calculate the appropriate modulo value based on
530-
// the desired number of digits for the HOTP Advanced. It is used in the truncation step of the
531-
// HOTP algorithm to ensure that the resulting HOTP Advanced value has the specified number of digits.
529+
// the desired number of digits for both the HOTP (HMAC-based One-Time Password) and
530+
// TOTP (Time-based One-Time Password) values. It is used in the truncation step of the
531+
// HOTP and TOTP algorithms to ensure that the resulting values have the specified number of digits.
532+
//
533+
// Magic Calculator, the function computes:
534+
//
535+
// 10ⁿ = 10 × 10ⁿ⁻¹, for n > 0
536+
// 10ⁿ = 1, for n ≤ 0
537+
//
538+
// where ⁿ denotes the exponentiation operation.
532539
func (v *Config) cryptoPow10n(n int) uint32 {
533540
if n <= 0 { // should be fine now, since this written in Go which it suitable for cryptographic.
534541
return 1

0 commit comments

Comments
 (0)