My favorites
▼
|
Sign in
csharptest-net
CSharpTest.Net's Code Library
Project Home
Downloads
Issues
Source
Repository:
default
wiki
Checkout
Browse
Changes
Clones
Source path:
hg
/
src
/
Library
/
Crypto
/
PasswordHash.cs
Branch:
default
Tag:
<none>
v1.10.1024.336
v1.10.1124.358
v1.10.420.164
v1.10.607.213
v1.10.913.269
v1.11.426.305
v1.11.924.348
v1.9.1004.144
‹c00ff02dcd55
4e0bd400d42a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#region Copyright 2010-2011 by Roger Knapp, Licensed under the Apache License, Version 2.0
/* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.IO;
using CSharpTest.Net.IO;
using System.Text;
using System.Collections;
using CSharpTest.Net.Bases;
using System.Security;
namespace CSharpTest.Net.Crypto
{
/// <summary> Creates a salted hash </summary>
public sealed class PasswordHash : Comparable<PasswordHash>, IDisposable
{
readonly SaltedData _hash;
/// <summary> Defines the derived hash iteration count used </summary>
public const int StandardIterations = 1024;
/// <summary> recreates a hash from a base-64 encoded string </summary>
public static PasswordHash FromString(string hash)
{ return FromBytes(Convert.FromBase64String(hash)); }
/// <summary> recreates a hash from the bytes returned by ToArray() </summary>
public static PasswordHash FromBytes(byte[] hash)
{ return new PasswordHash(new SaltedData(hash)); }
/// <summary> Recreates a hash </summary>
private PasswordHash(SaltedData hash)
{
_hash = hash;
}
/// <summary> Creates a salted hash from the given bytes and salt </summary>
public PasswordHash(Stream bytes, Salt salt)
{
using (bytes)
using (HashDerivedBytes<HMACSHA256> hashBytes = new HashDerivedBytes<HMACSHA256>(bytes, salt, StandardIterations))
_hash = new SaltedData(salt, hashBytes.GetBytes(32));
}
#region CTor overloads
/// <summary> Creates a salted hash from the given password </summary>
public PasswordHash(Password bytes)
: this(bytes.ReadBytes(), new Salt())
{ }
/// <summary> Creates a salted hash from the given password and salt </summary>
public PasswordHash(Password bytes, Salt salt)
: this(bytes.ReadBytes(), salt)
{ }
/// <summary> Creates the hash from the given bytes and salt </summary>
public PasswordHash(bool clear, byte[] bytes, Salt salt)
: this(new MemoryStream(bytes, 0, bytes.Length, false, false), salt)
{ if (clear) Array.Clear(bytes, 0, bytes.Length); }
/// <summary> Creates the hash from the given bytes </summary>
public PasswordHash(bool clear, byte[] bytes)
: this(clear, bytes, new Salt())
{ }
/// <summary> Creates the hash from the given data and salt </summary>
public PasswordHash(string data, Salt salt)
: this(true, Password.Encoding.GetBytes(data), salt)
{ }
/// <summary> Creates the hash from the given data and salt </summary>
public PasswordHash(string data)
: this(true, Password.Encoding.GetBytes(data), new Salt())
{ }
/// <summary> Creates the hash from the given data and salt </summary>
public PasswordHash(SecureString data, Salt salt)
: this(true, SecureStringUtils.ToByteArray(data, Password.Encoding), salt)
{ }
/// <summary> Creates the hash from the given data and salt </summary>
public PasswordHash(SecureString data)
: this(true, SecureStringUtils.ToByteArray(data, Password.Encoding), new Salt())
{ }
#endregion
/// <summary> Disposes of hash bytes </summary>
public void Dispose()
{ _hash.Dispose(); }
/// <summary> Gets the hash </summary>
protected override int HashCode { get { return BinaryComparer.GetHashCode(_hash.ToArray()); } }
/// <summary> Compares the hash </summary>
public override int CompareTo(PasswordHash other)
{
if (((object)other) == null)
return 1;
return BinaryComparer.Compare(_hash.ToArray(), other._hash.ToArray());
}
/// <summary> returns the salted-hash length in bytes </summary>
public int Length { get { return _hash.Length; } }
/// <summary> returns the salt used to create this hash </summary>
public Salt Salt { get { return _hash.Salt; } }
/// <summary> returns the salted-hash bytes </summary>
public byte[] ToArray() { return _hash.ToArray(); }
#region bool VerifyPassword(Password password)
/// <summary> Returns true if the provided password matches this hash </summary>
public bool VerifyPassword(Password password)
{
PasswordHash hash = password.CreateHash(Salt);
return this.Equals(hash);
}
/// <summary> Returns true if the provided password matches this hash </summary>
public bool VerifyPassword(Stream password)
{
PasswordHash hash = new PasswordHash(password, this.Salt);
return this.Equals(hash);
}
/// <summary> Returns true if the provided password matches this hash </summary>
public bool VerifyPassword(byte[] password)
{
using (Stream pb = new MemoryStream(password, 0, password.Length, false, false))
return VerifyPassword(pb);
}
/// <summary> Returns true if the provided password matches this hash </summary>
public bool VerifyPassword(string password)
{
byte[] tmp = Password.Encoding.GetBytes(password);
try { return VerifyPassword(tmp); }
finally { Array.Clear(tmp, 0, tmp.Length); }
}
#endregion
#region Object overrides
/// <summary> Returns the hash as a base-64 encoded string </summary>
public override string ToString()
{ return Convert.ToBase64String(_hash.ToArray()); }
#endregion
}
}
Show details
Hide details
Change log
59104bd26e63
by rogerk on Apr 26, 2011
Diff
Release version 1.11.426.305
Go to:
/.hgignore
...sTree.Test/BPlusTree.Test.csproj
...ree/BPlusTree.Test/BasicTests.cs
...ree.Test/SampleCustomTypeTest.cs
...ee.Test/SampleTypes/DataValue.cs
...mpleTypes/DataValueSerializer.cs
...Tree.Test/SampleTypes/KeyInfo.cs
...t/SampleTypes/KeyInfoComparer.cs
...SampleTypes/KeyInfoSerializer.cs
.../BPlusTree.Test/SequenceTests.cs
...ree.Test/TestBPlusTreeOptions.cs
...BPlusTree.Test/TestCollection.cs
...usTree.Test/TestCustomStorage.cs
...BPlusTree.Test/TestDictionary.cs
...sTree.Test/TestFileSerialized.cs
...ree.Test/TestMemorySerialized.cs
...ree.Test/TestSimpleDictionary.cs
...usTree.Test/ThreadedBTreeTest.cs
/src/BPlusTree/BPlusTree.csproj
.../Collections/BPlusTransaction.cs
...tions/BPlusTransactionFactory.cs
...e/Collections/BPlusTree.Debug.cs
...Collections/BPlusTree.Options.cs
...ollections/BPlusTree.Recovery.cs
...lusTree/Collections/BPlusTree.cs
...BPlusTree/Collections/Element.cs
...usTree/Collections/Enumerator.cs
...usTree/Collections/Interfaces.cs
...sTree/Collections/Node.Delete.cs
...sTree/Collections/Node.Insert.cs
...sTree/Collections/Node.Search.cs
...ee/Collections/Node.Serialize.cs
/src/BPlusTree/Collections/Node.cs
...ee/Collections/NodeCache.Base.cs
...ee/Collections/NodeCache.Full.cs
...ee/Collections/NodeCache.None.cs
.../Collections/NodeCache.Normal.cs
...usTree/Collections/NodeHandle.cs
...BPlusTree/Collections/NodePin.cs
...e/Collections/NodeTransaction.cs
/src/BPlusTree/Exceptions.cs
...sTree/Properties/AssemblyInfo.cs
.../BPlusTree/Resources.Designer.cs
/src/BPlusTree/Resources.resx
...PlusTree/Storage/Storage.Disk.cs
...usTree/Storage/Storage.Memory.cs
/src/CSBuild.exe
/src/CSBuild.xsd
/src/CmdTool.config
...g/UserSettingsSection.Upgrade.cs
Project members,
sign in
to write a code review
Older revisions
c00ff02dcd55
by Grigand on Jun 8, 2010
Diff
Release version 1.10.607.213
All revisions of this file
File info
Size: 6455 bytes, 147 lines
View raw file
Powered by
Google Project Hosting