Rediger

Del via


DataProtector.GetHashedPurpose Method

Definition

Creates a hash of the property values specified by the constructor.

protected:
 virtual cli::array <System::Byte> ^ GetHashedPurpose();
protected virtual byte[] GetHashedPurpose ();
abstract member GetHashedPurpose : unit -> byte[]
override this.GetHashedPurpose : unit -> byte[]
Protected Overridable Function GetHashedPurpose () As Byte()

Returns

Byte[]

An array of bytes that contain the hash of the ApplicationName, PrimaryPurpose, and SpecificPurposes properties.

Examples

The following example shows how to use the GetHashedPurpose method to hash the purpose properties for use as extra entropy. It is part of a larger code example for the DataProtector class.

// To allow a service to hand out instances of a DataProtector we demand unrestricted DataProtectionPermission
// in the constructor, but Assert the permission when ProviderProtect is called.  This is similar to FileStream
// where access is checked at time of creation, not time of use.
[SecuritySafeCritical]
[DataProtectionPermission(SecurityAction.Assert, ProtectData = true)]
protected override byte[] ProviderProtect(byte[] userData)
{
    // Delegate to ProtectedData
    return ProtectedData.Protect(userData, GetHashedPurpose(), Scope);
}
' To allow a service to hand out instances of a DataProtector we demand unrestricted DataProtectionPermission 
' in the constructor, but Assert the permission when ProviderProtect is called.  This is similar to FileStream
' where access is checked at time of creation, not time of use.
<SecuritySafeCritical(), DataProtectionPermission(SecurityAction.Assert, ProtectData:=True)> _
Protected Overrides Function ProviderProtect(ByVal userData() As Byte) As Byte()
    ' Delegate to ProtectedData
    Return ProtectedData.Protect(userData, GetHashedPurpose(), Scope)

End Function 'ProviderProtect

Remarks

GetHashedPurpose computes the hash of the application name and the full purpose. The full purpose is a concatenation of all the PrimaryPurpose and SpecificPurposes properties. Each of the three parts is prefaced with its length so the hash can be reversed.

Applies to