-
Notifications
You must be signed in to change notification settings - Fork 1
/
Fingercrypt.cs
170 lines (128 loc) · 6.17 KB
/
Fingercrypt.cs
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
163
164
165
166
167
168
169
170
using System;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using OpenCvSharp;
using System.Diagnostics;
namespace Fingercrypt
{
public class Fingercrypt
{
public static string HashFingerprint(Mat img, bool inverseColors)
{
if (inverseColors)
Cv2.BitwiseNot(img, img);
Cv2.Threshold(img, img, 200, 255, ThresholdTypes.Binary);
var lines = GetImageLines(img);
return BitConverter.ToString(HashFingerprint(lines)).Replace("-", "");
}
public static byte[] HashFingerprint(LineSegmentPoint[] lines, int linesPerChunk=1, int chunkSize=16)
{
var stream = new MemoryStream();
using (var sha512 = new SHA512Managed())
{
foreach (var lineChunk in lines.Split(linesPerChunk))
{
var plainChunk = new StringBuilder();
foreach (var line in lineChunk)
{
plainChunk.Append(line.P1.X);
plainChunk.Append(line.P1.Y);
plainChunk.Append(line.P2.X);
plainChunk.Append(line.P2.Y);
}
var hash = sha512.ComputeHash(Encoding.UTF8.GetBytes(plainChunk.ToString()));
stream.Write(hash);
}
}
return stream.ToArray();
}
public static bool CheckFingerprint(string hashedLines, LineSegmentPoint[] lines, float chunkPercentThresold,
int chunkLength = 128, int linesPerChunk = 1, int allowedVariation = 1)
{
var chunks = lines.Split(linesPerChunk).ToArray();
var matchingChunks = 0;
var numberOfChunks = chunks.Length;
using (var sha512 = new SHA512Managed())
{
var currentChunk = 0;
foreach (var hashedLineChunk in hashedLines.SplitByLength(chunkLength))
{
foreach (var possibleOffset in Enumerable.Range(0, allowedVariation + 1)
.Combinations(linesPerChunk * 4))
{
if (currentChunk >= numberOfChunks) break;
//Positive Side
var currentOffsetValue = 0;
var plainChunk = new StringBuilder();
foreach (var line in chunks[currentChunk])
{
plainChunk.Append(line.P1.X + char.GetNumericValue(possibleOffset[currentOffsetValue]));
plainChunk.Append(line.P1.Y + char.GetNumericValue(possibleOffset[currentOffsetValue + 1]));
plainChunk.Append(line.P2.X + char.GetNumericValue(possibleOffset[currentOffsetValue + 2]));
plainChunk.Append(line.P2.Y + char.GetNumericValue(possibleOffset[currentOffsetValue + 3]));
currentOffsetValue += 4;
}
var hash = sha512.ComputeHash(Encoding.UTF8.GetBytes(plainChunk.ToString()));
if (BitConverter.ToString(hash).Replace("-", "") == hashedLineChunk)
{
matchingChunks += 1;
break;
}
//Negative Side
currentOffsetValue = 0;
plainChunk = new StringBuilder();
foreach (var line in chunks[currentChunk])
{
plainChunk.Append(line.P1.X + char.GetNumericValue(possibleOffset[currentOffsetValue]));
plainChunk.Append(line.P1.Y + char.GetNumericValue(possibleOffset[currentOffsetValue + 1]));
plainChunk.Append(line.P2.X + char.GetNumericValue(possibleOffset[currentOffsetValue + 2]));
plainChunk.Append(line.P2.Y + char.GetNumericValue(possibleOffset[currentOffsetValue + 3]));
currentOffsetValue += 4;
}
hash = sha512.ComputeHash(Encoding.UTF8.GetBytes(plainChunk.ToString()));
if (BitConverter.ToString(hash).Replace("-", "") == hashedLineChunk)
{
matchingChunks += 1;
break;
}
}
currentChunk++;
}
}
return matchingChunks / numberOfChunks >= chunkPercentThresold/100;
}
public static LineSegmentPoint[] GetImageLines(Mat img)
{
var skeleton = new Mat(img.Size(), MatType.CV_8UC1, new Scalar(0));
Cv2.Canny(img, skeleton, 255/3, 255);
var unCroppedLines = Cv2.HoughLinesP(skeleton, 1, Cv2.PI / 180, 15, 0);
var minX = int.MaxValue;
var maxX = int.MinValue;
var minY = int.MaxValue;
var maxY = int.MinValue;
foreach (var lines in unCroppedLines)
{
if (lines.P1.X < minX)
minX = lines.P1.X;
if (lines.P2.X < minX)
minX = lines.P2.X;
if (lines.P1.X > maxX)
maxX = lines.P1.X;
if (lines.P2.X > maxX)
maxX = lines.P2.X;
if (lines.P1.Y < minY)
minY = lines.P1.Y;
if (lines.P2.Y < minY)
minY = lines.P2.Y;
if (lines.P1.Y > maxY)
maxY = lines.P1.Y;
if (lines.P2.Y > maxY)
maxY = lines.P2.Y;
}
var croppedImage = new Mat(skeleton, Rect.FromLTRB(minX, minY, maxX, maxY));
return Cv2.HoughLinesP(croppedImage, 1, Cv2.PI / 180, 15, 10, 5);
}
}
}