After imported TOTP
in your file, you can access to all its methods.
import { TOTP } from '@akanass/rx-otp';
Generates TOTP
token for given key
.
Parameters:
- {string} key (required): Key for the one time password. This should be unique and secret for every user as this is the seed that is used to calculate the HMAC. Format can be
ASCII
orHEX
.- {TOTPGenerateOptions} options (optional): object contains
key_format
,time
,timestamp
,code_digits
,add_checksum
,truncation_offset
andalgorithm
. (default:empty object
).
Response:
{Observable} - A numeric string
in base 10
includes code_digits
plus the optional checksum
digit if requested.
Example:
TOTP.generate('12345678901234567890', { timestamp: 59000, code_digits: 8, algorithm: 'SHA1' }).subscribe({
next: token => console.log(token), // display 94287082 in the console
error: err => console.error(err) // show error in console
});
Verifies TOTP
token with given key
.
Parameters:
- {string} token (required): Passcode to validate.
- {string} key (required): Key for the one time password. This should be unique and secret for every user as this is the seed that is used to calculate the HMAC. Format can be
ASCII
orHEX
.- {TOTPVerifyOptions} options (optional): object contains
key_format
,window
,time
,timestamp
,add_checksum
,truncation_offset
andalgorithm
. (default:empty object
).
Response:
{Observable} - OTPVerifyResult
with delta
and delta_format: 'int'
if the token
is valid
else throw
an exception
Example:
TOTP.verify('94287082', '3132333435363738393031323334353637383930', { key_format: 'hex', timestamp: 59000, algorithm: 'SHA1' }).subscribe({
next: data => console.log(data), // display {delta: 0, delta_format: 'int'} in the console
error: err => console.error(err) // show error in console
});
- {enum} key_format (optional): The format of the key which can be
'str'
for anASCII string
or'hex'
for ahexadecimal string
. (default'str'
)- {number} time (optional): The time step of the counter. This must be the same for every request and is used to calculate C. (default
30
)- {number} timestamp (optional): OTP validity
timestamp
. (defaultcurrent datetime
)- {number} code_digits (optional): The number of
digits
in theOTP
, not including the checksum, if any. (default6
)- {boolean} add_checksum (optional): A flag indicates if a
checksum digit
should be appended to theOTP
. (defaultfalse
)- {number} truncation_offset (optional): The
offset
into the MAC result to begintruncation
. Between0
and15
. (default-1
to delete it)- {enum} algorithm (optional): The algorithm to create HMAC:
'SHA1'
|'SHA256'
|'SHA512'
. (default'SHA512'
)
- {enum} key_format (optional): The format of the key which can be
'str'
for anASCII string
or'hex'
for ahexadecimal string
. (default'str'
)- {number} window (optional): The allowable margin,
time steps
in seconds sinceT0
, for the counter. The function will check 'W' codes in the future against the provided passcode. IfW = 1
, andT = 30
, this function will check the passcode against all One Time Passcodes between-30s
and+30s
. (default1
)- {number} time (optional): The time step of the counter. This must be the same for every request and is used to calculate C. (default
30
)- {number} timestamp (optional): OTP validity
timestamp
. (defaultcurrent datetime
)- {boolean} add_checksum (optional): A flag indicates if a
checksum digit
should be appended to theOTP
. (defaultfalse
)- {number} truncation_offset (optional): The
offset
into the MAC result to begintruncation
. Between0
and15
. (default-1
to delete it)- {enum} algorithm (optional): The algorithm to create HMAC:
'SHA1'
|'SHA256'
|'SHA512'
. (default'SHA512'
)
- {number | string} delta (required): The
delta
with thecounter
during thevalidation
.- {enum} delta_format (required): The
delta format
which can be'int'
for anumber
or'hex'
for ahexadecimal string
. This value is the same thancounter_format
inTOTPVerifyOptions
.
- Implementation of all methods (2019-03-08)