protected void configureSettings(Schema schemaVersion) { switch (schemaVersion) { case Schema.V0: aesMode = AesMode.CTR; options = Options.V0; hmac_includesHeader = false; hmac_includesPadding = true; hmac_algorithm = HmacAlgorithm.SHA1; break; case Schema.V1: aesMode = AesMode.CBC; options = Options.V1; hmac_includesHeader = false; hmac_includesPadding = false; hmac_algorithm = HmacAlgorithm.SHA256; break; case Schema.V2: case Schema.V3: aesMode = AesMode.CBC; options = Options.V1; hmac_includesHeader = true; hmac_includesPadding = false; hmac_algorithm = HmacAlgorithm.SHA256; break; } }
/// <summary> /// Create an Instance of a CredentialService /// </summary> /// <param name="dragonchainId">dragonchainId associated with these credentials</param> /// <param name="authKey">authKey to use with these credentials</param> /// <param name="authKeyId">authKeyId to use with these credentials</param> /// <param name="hmacAlgo">hmac algorithm to use</param> /// <param name="credentialManager">manager to retrieve Dragonchain credentials from config provider</param> /// <param name="logger"></param> public CredentialService(string dragonchainId, string authKey = "", string authKeyId = "", string endpointUrl = "", HmacAlgorithm hmacAlgo = HmacAlgorithm.SHA256, ICredentialManager credentialManager = null, ILogger <DragonchainClient> logger = null) { _logger = logger ?? new NullLogger <DragonchainClient>(); DragonchainId = dragonchainId; if (!string.IsNullOrWhiteSpace(authKey) && !string.IsNullOrWhiteSpace(authKeyId)) { _logger.LogDebug("Auth Key/Id and endpoint URL provided explicitly, will not search env/disk"); _credentials = new DragonchainCredentials { AuthKey = authKey, AuthKeyId = authKeyId, EndpointUrl = endpointUrl }; } else { try { _credentials = credentialManager.GetDragonchainCredentials(dragonchainId); } catch { // don't require credentials to be present on construction _credentials = new DragonchainCredentials { AuthKey = string.Empty, AuthKeyId = string.Empty, EndpointUrl = string.Empty }; } } _hmacAlgo = hmacAlgo; }
private byte[] ComputeHmac(HmacAlgorithm alg, byte[] payload, uint seq) { using (var worker = new SshDataWorker()) { worker.Write(seq); worker.Write(payload); return(alg.ComputeHash(worker.ToByteArray())); } }
public static string ToValue(this HmacAlgorithm hmacAlgo) { switch (hmacAlgo) { case HmacAlgorithm.SHA3_256: return("SHA3-256"); default: return(hmacAlgo.ToString()); } }
/// <summary> /// Creates new time-based generator settings. /// </summary> /// <param name="label">Label for this generator.</param> /// <param name="issuer">Issuer for this generator.</param> /// <param name="secret">Secret for this generator.</param> /// <param name="encoding">Encoding of the secret.</param> /// <param name="algo">HMAC algorithm of the generator.</param> /// <param name="digits">Number of digits in generated codes.</param> /// <param name="additional">Additional data to feed to challenge generator.</param> /// <param name="counter">Current value of the counter.</param> public HotpGeneratorSettings( string label, string issuer, byte[] secret, ByteEncoding encoding, HmacAlgorithm algo, int digits, byte[] additional, long counter) : base(ChallengeType.Counter, label, issuer, secret, encoding, algo, digits, additional) { Volatile.Write(ref this._counter, counter); }
/// <summary> /// Creates new time-based generator settings. /// </summary> /// <param name="label">Label for this generator.</param> /// <param name="issuer">Issuer for this generator.</param> /// <param name="secret">Secret for this generator.</param> /// <param name="encoding">Encoding of the secret.</param> /// <param name="algo">HMAC algorithm of the generator.</param> /// <param name="digits">Number of digits in generated codes.</param> /// <param name="additional">Additional data to feed to challenge generator.</param> /// <param name="period">Number of seconds in a single generator tick.</param> public TotpGeneratorSettings( string label, string issuer, byte[] secret, ByteEncoding encoding, HmacAlgorithm algo, int digits, byte[] additional, int period) : base(ChallengeType.Time, label, issuer, secret, encoding, algo, digits, additional) { this.Period = period; }
private HashAlgorithm CreateHmac(HmacAlgorithm hmacAlgo, string authKey) { var authKeyBytes = Encoding.UTF8.GetBytes(authKey); switch (hmacAlgo) { case HmacAlgorithm.BLAKE2b512: case HmacAlgorithm.SHA3_256: throw new NotSupportedException(); default: return(new HMACSHA256(authKeyBytes)); } }
internal static TotpGeneratorSettings FinishParsingUri( string label, string issuer, byte[] secret, ByteEncoding encoding, HmacAlgorithm algo, int digits, byte[] additional, IDictionary <string, StringValues> args) { var periodr = args.TryGetValue(ParamPeriod, out var periods) ? periods.First() : "30"; return(new TotpGeneratorSettings(label, issuer, secret, encoding, algo, digits, additional, int.Parse(periodr, NumberStyles.Integer, CultureInfo.InvariantCulture))); }
internal static HotpGeneratorSettings FinishParsingUri( string label, string issuer, byte[] secret, ByteEncoding encoding, HmacAlgorithm algo, int digits, byte[] additional, IDictionary<string, StringValues> args) { if (!args.TryGetValue(ParamCounter, out var counters)) throw new ArgumentException("Missing counter value.", nameof(args)); return new HotpGeneratorSettings(label, issuer, secret, encoding, algo, digits, additional, long.Parse(counters.First(), NumberStyles.Integer, CultureInfo.InvariantCulture)); }
internal OtpGeneratorSettings( ChallengeType type, string label, string issuer, byte[] secret, ByteEncoding encoding, HmacAlgorithm algo, int digits, byte[] additional) { this.Type = type; this.Label = label; this.Issuer = issuer; this.Secret = ImmutableArray.Create(secret); this.SecretEncoding = encoding; this.Algorithm = algo; this.Digits = digits; this.Additional = additional != null ? ImmutableArray.Create(additional) : ImmutableArray <byte> .Empty; }
// Token: 0x0600000B RID: 11 RVA: 0x000044B8 File Offset: 0x000026B8 protected byte[] generateHmac(PayloadComponents components, string password) { List <byte> list = new List <byte>(); bool flag = this.hmac_includesHeader; if (flag) { list.AddRange(this.assembleHeader(components)); } list.AddRange(components.ciphertext); byte[] key = this.generateKey(components.hmacSalt, password); HMAC hmac = null; HmacAlgorithm hmacAlgorithm = this.hmac_algorithm; if (hmacAlgorithm != HmacAlgorithm.SHA1) { if (hmacAlgorithm == HmacAlgorithm.SHA256) { hmac = new HMACSHA256(key); } } else { hmac = new HMACSHA1(key); } List <byte> list2 = new List <byte>(); list2.AddRange(hmac.ComputeHash(list.ToArray())); bool flag2 = this.hmac_includesPadding; if (flag2) { for (int i = list2.Count; i < 32; i++) { list2.Add(0); } } return(list2.ToArray()); }
/// <summary> /// This method generates a TOTP value for the given set of parameters. /// </summary> /// <param name="key">the shared secret</param> /// <param name="timeStep">timestamp step</param> /// <param name="codeDigits">number of digits to totp</param> /// <param name="algorithm">algorithm of hash</param> /// <returns>totp</returns> public static int GenerateTOTP(byte[] key, int timeStep = 30, int codeDigits = 6, HmacAlgorithm algorithm = HmacAlgorithm.HMACSHA1) { int divisor = Convert.ToInt32(Math.Pow(10, codeDigits)); var data = BitConverter.GetBytes(GetTimestamp(timeStep)).Reverse().ToArray(); string algorithmName = Enum.GetName(typeof(HmacAlgorithm), algorithm); HMAC hmac = HMAC.Create(algorithmName); hmac.Key = key; byte[] hash = hmac.ComputeHash(data); int offset = hash.Last() & 0x0F; int oneTimePassword = ( ((hash[offset + 0] & 0x7f) << 24) | ((hash[offset + 1] & 0xff) << 16) | ((hash[offset + 2] & 0xff) << 8) | (hash[offset + 3] & 0xff) ) % divisor; return(oneTimePassword); }
/// <summary> /// Creates a MAC provider instance based on its Id. /// </summary> /// <param name="algo">Algorithm Id.</param> /// <returns>Created MAC provider instance.</returns> /// <exception cref="ArgumentException">Unrecognized value specified for <paramref name="algo"/>.</exception> public static IHmacProvider FromId(HmacAlgorithm algo) => algo switch {