Repository navigation
Expand file tree
/
Copy path_sha.cs
More file actions
162 lines (134 loc) · 5.43 KB
/
Copy path_sha.cs
File metadata and controls
162 lines (134 loc) · 5.43 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* [email protected]. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using Microsoft.Scripting.Runtime;
using IronPython.Runtime;
using IronPython.Runtime.Operations;
//!!! This is pretty inefficient. We should probably use hasher.TransformBlock instead of
//!!! hanging onto all of the bytes.
//!!! Also, we could probably make a generic version of this that could then be specialized
//!!! for both md5 and sha.
[assembly: PythonModule("_sha", typeof(IronPython.Modules.PythonSha))]
namespace IronPython.Modules {
public static class PythonSha {
public const string __doc__ = "implements the SHA1 hash algorithm";
[ThreadStatic]
private static SHA1Managed _hasher;
private const int blockSize = 64;
private static SHA1Managed GetHasher() {
if (_hasher == null) {
_hasher = new SHA1Managed();
}
return _hasher;
}
public static int digest_size {
[Documentation("Size of the resulting digest in bytes (constant)")]
get { return GetHasher().HashSize / 8; }
}
public static int digestsize {
[Documentation("Size of the resulting digest in bytes (constant)")]
get { return digest_size; }
}
public static int blocksize {
[Documentation("Block size")]
get { return blockSize; }
}
[Documentation("new([data]) -> object (object used to calculate hash)")]
public static sha @new(object data) {
return new sha(data);
}
[Documentation("new([data]) -> object (object used to calculate hash)")]
public static sha @new(Bytes data) {
return new sha((IList<byte>)data);
}
[Documentation("new([data]) -> object (object used to calculate hash)")]
public static sha @new(PythonBuffer data) {
return new sha(data);
}
[Documentation("new([data]) -> object (object used to calculate hash)")]
public static sha @new(ByteArray data) {
return new sha((IList<byte>)data);
}
[Documentation("new([data]) -> object (object used to calculate hash)")]
public static sha @new() {
return new sha();
}
[Documentation("new([data]) -> object (object used to calculate hash)")]
[PythonType, PythonHidden]
public class sha
#if FEATURE_ICLONEABLE
: ICloneable
#endif
{
byte[] _bytes;
byte[] _hash;
public static readonly int digest_size = PythonSha.digest_size;
public static readonly int block_size = PythonSha.blocksize;
public sha() : this(new byte[0]) { }
public sha(object initialData) {
_bytes = new byte[0];
update(initialData);
}
internal sha(IList<byte> initialBytes) {
_bytes = new byte[0];
update(initialBytes);
}
[Documentation("update(string) -> None (update digest with string data)")]
public void update(object newData) {
update(Converter.ConvertToString(newData).MakeByteArray());
}
public void update(Bytes newBytes) {
update((IList<byte>)newBytes);
}
public void update(PythonBuffer newBytes) {
update((IList<byte>)newBytes);
}
public void update(ByteArray newBytes) {
update((IList<byte>)newBytes);
}
private void update(IList<byte> newBytes) {
byte[] updatedBytes = new byte[_bytes.Length + newBytes.Count];
Array.Copy(_bytes, updatedBytes, _bytes.Length);
newBytes.CopyTo(updatedBytes, _bytes.Length);
_bytes = updatedBytes;
_hash = GetHasher().ComputeHash(_bytes);
}
[Documentation("digest() -> int (current digest value)")]
public string digest() {
return _hash.MakeString();
}
[Documentation("hexdigest() -> string (current digest as hex digits)")]
public string hexdigest() {
StringBuilder result = new StringBuilder(2 * _hash.Length);
for (int i = 0; i < _hash.Length; i++) {
result.Append(_hash[i].ToString("x2"));
}
return result.ToString();
}
[Documentation("copy() -> object (copy of this object)")]
public sha copy() {
return new sha(_bytes);
}
#if FEATURE_ICLONEABLE
object ICloneable.Clone() {
return copy();
}
#endif
}
}
}