コード例 #1
0
        /// <summary>
        /// Returns the length of the shared secret (in bytes).
        /// </summary>
        /// <param name="protocol">The protocol version being used that will be used to lookup the text in <paramref name="associationType"/></param>
        /// <param name="associationType">The value of the protocol argument specifying the type of association.  For example: "HMAC-SHA1".</param>
        /// <returns>The length (in bytes) of the association secret.</returns>
        /// <exception cref="ProtocolException">Thrown if no association can be found by the given name.</exception>
        public static int GetSecretLength(Protocol protocol, string associationType)
        {
            HmacSha match = hmacShaAssociationTypes.FirstOrDefault(shaType => String.Equals(shaType.GetAssociationType(protocol), associationType, StringComparison.Ordinal));

            ErrorUtilities.VerifyProtocol(match != null, OpenIdStrings.NoAssociationTypeFoundByName, associationType);
            return(match.SecretLength);
        }
コード例 #2
0
ファイル: HmacShaAssociation.cs プロジェクト: tt/dotnetopenid
        HmacShaAssociation(HmacSha typeIdentity, string handle, byte[] secret, TimeSpan totalLifeLength)
            : base(handle, secret, totalLifeLength, DateTime.UtcNow)
        {
            if (typeIdentity == null) throw new ArgumentNullException("typeIdentity");

            Debug.Assert(secret.Length == typeIdentity.SecretLength);
            this.typeIdentity = typeIdentity;
        }
コード例 #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HmacShaAssociation"/> class.
        /// </summary>
        /// <param name="typeIdentity">The specific variety of HMAC-SHA this association is based on (whether it be HMAC-SHA1, HMAC-SHA256, etc.)</param>
        /// <param name="handle">The association handle.</param>
        /// <param name="secret">The association secret.</param>
        /// <param name="totalLifeLength">The time duration the association will be good for.</param>
        private HmacShaAssociation(HmacSha typeIdentity, string handle, byte[] secret, TimeSpan totalLifeLength)
            : base(handle, secret, totalLifeLength, DateTime.UtcNow)
        {
            ErrorUtilities.VerifyArgumentNotNull(typeIdentity, "typeIdentity");
            ErrorUtilities.VerifyNonZeroLength(handle, "handle");
            ErrorUtilities.VerifyArgumentNotNull(secret, "secret");
            ErrorUtilities.VerifyProtocol(secret.Length == typeIdentity.SecretLength, OpenIdStrings.AssociationSecretAndTypeLengthMismatch, secret.Length, typeIdentity.GetAssociationType(Protocol.Default));

            this.typeIdentity = typeIdentity;
        }
コード例 #4
0
        /// <summary>
        /// Creates an association with the specified handle, secret, and lifetime.
        /// </summary>
        /// <param name="handle">The handle.</param>
        /// <param name="secret">The secret.</param>
        /// <param name="totalLifeLength">Total lifetime.</param>
        /// <returns>The newly created association.</returns>
        public static HmacShaAssociation Create(string handle, byte[] secret, TimeSpan totalLifeLength)
        {
            ErrorUtilities.VerifyNonZeroLength(handle, "handle");
            ErrorUtilities.VerifyArgumentNotNull(secret, "secret");

            HmacSha shaType = hmacShaAssociationTypes.FirstOrDefault(sha => sha.SecretLength == secret.Length);

            ErrorUtilities.VerifyProtocol(shaType != null, OpenIdStrings.NoAssociationTypeFoundByLength, secret.Length);
            return(new HmacShaAssociation(shaType, handle, secret, totalLifeLength));
        }
コード例 #5
0
 public string GetSign(string text)
 {
     byte[] data = Encoding.ASCII.GetBytes(text);
     byte[] hash = HmacSha.ComputeHash(data);
     return(BitConverter.ToString(hash).Replace("-", string.Empty).ToLower());
     //StringBuilder builder = new StringBuilder();
     //for(int i = 0; i < hash.Length; i++)
     //    builder.Append(hash[i].ToString("x2", CultureInfo.InvariantCulture));
     //return builder.ToString();
 }
コード例 #6
0
		/// <summary>
		/// Initializes a new instance of the <see cref="HmacShaAssociation"/> class.
		/// </summary>
		/// <param name="typeIdentity">The specific variety of HMAC-SHA this association is based on (whether it be HMAC-SHA1, HMAC-SHA256, etc.)</param>
		/// <param name="handle">The association handle.</param>
		/// <param name="secret">The association secret.</param>
		/// <param name="totalLifeLength">The time duration the association will be good for.</param>
		private HmacShaAssociation(HmacSha typeIdentity, string handle, byte[] secret, TimeSpan totalLifeLength)
			: base(handle, secret, totalLifeLength, DateTime.UtcNow) {
			Requires.NotNull(typeIdentity, "typeIdentity");
			Requires.NotNullOrEmpty(handle, "handle");
			Requires.NotNull(secret, "secret");
			Requires.Range(totalLifeLength > TimeSpan.Zero, "totalLifeLength");
			ErrorUtilities.VerifyProtocol(secret.Length == typeIdentity.SecretLength, OpenIdStrings.AssociationSecretAndTypeLengthMismatch, secret.Length, typeIdentity.GetAssociationType(Protocol.Default));

			this.typeIdentity = typeIdentity;
		}
コード例 #7
0
        /// <summary>
        /// Creates an HMAC-SHA association.
        /// </summary>
        /// <param name="protocol">The OpenID protocol version that the request for an association came in on.</param>
        /// <param name="associationType">The value of the openid.assoc_type parameter.</param>
        /// <param name="handle">The association handle.</param>
        /// <param name="secret">The association secret.</param>
        /// <param name="totalLifeLength">How long the association will be good for.</param>
        /// <returns>The newly created association.</returns>
        public static HmacShaAssociation Create(Protocol protocol, string associationType, string handle, byte[] secret, TimeSpan totalLifeLength)
        {
            Requires.NotNull(protocol, "protocol");
            Requires.NotNullOrEmpty(associationType, "associationType");
            Requires.NotNull(secret, "secret");
            HmacSha match = hmacShaAssociationTypes.FirstOrDefault(sha => string.Equals(sha.GetAssociationType(protocol), associationType, StringComparison.Ordinal));

            ErrorUtilities.VerifyProtocol(match != null, OpenIdStrings.NoAssociationTypeFoundByName, associationType);
            return(new HmacShaAssociation(match, handle, secret, totalLifeLength));
        }
コード例 #8
0
        /// <summary>
        /// Creates an association with the specified handle, secret, and lifetime.
        /// </summary>
        /// <param name="handle">The handle.</param>
        /// <param name="secret">The secret.</param>
        /// <param name="totalLifeLength">Total lifetime.</param>
        /// <returns>The newly created association.</returns>
        public static HmacShaAssociation Create(string handle, byte[] secret, TimeSpan totalLifeLength)
        {
            Requires.NotNullOrEmpty(handle, "handle");
            Requires.NotNull(secret, "secret");
            Contract.Ensures(Contract.Result <HmacShaAssociation>() != null);

            HmacSha shaType = hmacShaAssociationTypes.FirstOrDefault(sha => sha.SecretLength == secret.Length);

            ErrorUtilities.VerifyProtocol(shaType != null, OpenIdStrings.NoAssociationTypeFoundByLength, secret.Length);
            return(new HmacShaAssociation(shaType, handle, secret, totalLifeLength));
        }
コード例 #9
0
		/// <summary>
		/// Initializes a new instance of the <see cref="HmacShaAssociation"/> class.
		/// </summary>
		/// <param name="typeIdentity">The specific variety of HMAC-SHA this association is based on (whether it be HMAC-SHA1, HMAC-SHA256, etc.)</param>
		/// <param name="handle">The association handle.</param>
		/// <param name="secret">The association secret.</param>
		/// <param name="totalLifeLength">The time duration the association will be good for.</param>
		private HmacShaAssociation(HmacSha typeIdentity, string handle, byte[] secret, TimeSpan totalLifeLength)
			: base(handle, secret, totalLifeLength, DateTime.UtcNow) {
			Contract.Requires<ArgumentNullException>(typeIdentity != null);
			Contract.Requires<ArgumentException>(!String.IsNullOrEmpty(handle));
			Contract.Requires<ArgumentNullException>(secret != null);
			Contract.Requires<ArgumentOutOfRangeException>(totalLifeLength > TimeSpan.Zero);
			Contract.Ensures(this.TotalLifeLength == totalLifeLength);
			ErrorUtilities.VerifyProtocol(secret.Length == typeIdentity.SecretLength, OpenIdStrings.AssociationSecretAndTypeLengthMismatch, secret.Length, typeIdentity.GetAssociationType(Protocol.Default));

			this.typeIdentity = typeIdentity;
		}
コード例 #10
0
        HmacShaAssociation(HmacSha typeIdentity, string handle, byte[] secret, TimeSpan totalLifeLength)
            : base(handle, secret, totalLifeLength, DateTime.UtcNow)
        {
            if (typeIdentity == null)
            {
                throw new ArgumentNullException("typeIdentity");
            }

            Debug.Assert(secret.Length == typeIdentity.SecretLength);
            this.typeIdentity = typeIdentity;
        }
コード例 #11
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HmacShaAssociation"/> class.
        /// </summary>
        /// <param name="typeIdentity">The specific variety of HMAC-SHA this association is based on (whether it be HMAC-SHA1, HMAC-SHA256, etc.)</param>
        /// <param name="handle">The association handle.</param>
        /// <param name="secret">The association secret.</param>
        /// <param name="totalLifeLength">The time duration the association will be good for.</param>
        private HmacShaAssociation(HmacSha typeIdentity, string handle, byte[] secret, TimeSpan totalLifeLength)
            : base(handle, secret, totalLifeLength, DateTime.UtcNow)
        {
            Requires.NotNull(typeIdentity, "typeIdentity");
            Requires.NotNullOrEmpty(handle, "handle");
            Requires.NotNull(secret, "secret");
            Requires.InRange(totalLifeLength > TimeSpan.Zero, "totalLifeLength");
            Contract.Ensures(this.TotalLifeLength == totalLifeLength);
            ErrorUtilities.VerifyProtocol(secret.Length == typeIdentity.SecretLength, OpenIdStrings.AssociationSecretAndTypeLengthMismatch, secret.Length, typeIdentity.GetAssociationType(Protocol.Default));

            this.typeIdentity = typeIdentity;
        }
コード例 #12
0
        public string GetSign(string text)
        {
            byte[]        data    = Encoding.UTF8.GetBytes(text);
            byte[]        hash    = HmacSha.ComputeHash(data, 0, data.Length);
            StringBuilder builder = new StringBuilder();

            for (int i = 0; i < hash.Length; i++)
            {
                builder.Append(hash[i].ToString("x2", CultureInfo.InvariantCulture));
            }
            return(builder.ToString());
        }
コード例 #13
0
        /// <summary>
        /// Initializes a new instance of the <see cref="HmacShaAssociation"/> class.
        /// </summary>
        /// <param name="typeIdentity">The specific variety of HMAC-SHA this association is based on (whether it be HMAC-SHA1, HMAC-SHA256, etc.)</param>
        /// <param name="handle">The association handle.</param>
        /// <param name="secret">The association secret.</param>
        /// <param name="totalLifeLength">The time duration the association will be good for.</param>
        private HmacShaAssociation(HmacSha typeIdentity, string handle, byte[] secret, TimeSpan totalLifeLength)
            : base(handle, secret, totalLifeLength, DateTime.UtcNow)
        {
            Contract.Requires <ArgumentNullException>(typeIdentity != null);
            Contract.Requires <ArgumentException>(!String.IsNullOrEmpty(handle));
            Contract.Requires <ArgumentNullException>(secret != null);
            Contract.Requires <ArgumentOutOfRangeException>(totalLifeLength > TimeSpan.Zero);
            Contract.Ensures(this.TotalLifeLength == totalLifeLength);
            ErrorUtilities.VerifyProtocol(secret.Length == typeIdentity.SecretLength, OpenIdStrings.AssociationSecretAndTypeLengthMismatch, secret.Length, typeIdentity.GetAssociationType(Protocol.Default));

            this.typeIdentity = typeIdentity;
        }
コード例 #14
0
        /// <summary>
        /// Creates an HMAC-SHA association.
        /// </summary>
        /// <param name="protocol">The OpenID protocol version that the request for an association came in on.</param>
        /// <param name="associationType">The value of the openid.assoc_type parameter.</param>
        /// <param name="handle">The association handle.</param>
        /// <param name="secret">The association secret.</param>
        /// <param name="totalLifeLength">How long the association will be good for.</param>
        /// <returns>The newly created association.</returns>
        public static HmacShaAssociation Create(Protocol protocol, string associationType, string handle, byte[] secret, TimeSpan totalLifeLength)
        {
            Contract.Requires <ArgumentNullException>(protocol != null);
            Contract.Requires <ArgumentException>(!String.IsNullOrEmpty(associationType));
            Contract.Requires <ArgumentNullException>(secret != null);
            Contract.Ensures(Contract.Result <HmacShaAssociation>() != null);

            HmacSha match = hmacShaAssociationTypes.FirstOrDefault(sha => String.Equals(sha.GetAssociationType(protocol), associationType, StringComparison.Ordinal));

            ErrorUtilities.VerifyProtocol(match != null, OpenIdStrings.NoAssociationTypeFoundByName, associationType);
            return(new HmacShaAssociation(match, handle, secret, totalLifeLength));
        }