Add codes for MD5(IP) distribution

This commit is contained in:
Valentin Brandl 2022-03-29 20:37:56 +02:00
parent acee05a161
commit d1d76a2dba
11 changed files with 689 additions and 0 deletions

1
codes/md5-dist/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/target

70
codes/md5-dist/Cargo.lock generated Normal file
View File

@ -0,0 +1,70 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "block-buffer"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0bf7fe51849ea569fd452f37822f606a5cabb684dc918707a0193fd4664ff324"
dependencies = [
"generic-array",
]
[[package]]
name = "crypto-common"
version = "0.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "57952ca27b5e3606ff4dd79b0020231aaf9d6aa76dc05fd30137538c50bd3ce8"
dependencies = [
"generic-array",
"typenum",
]
[[package]]
name = "digest"
version = "0.10.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2fb860ca6fafa5552fb6d0e816a69c8e49f0908bf524e30a90d97c85892d506"
dependencies = [
"block-buffer",
"crypto-common",
]
[[package]]
name = "generic-array"
version = "0.14.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd48d33ec7f05fbfa152300fdad764757cbded343c1aa1cff2fbaf4134851803"
dependencies = [
"typenum",
"version_check",
]
[[package]]
name = "md-5"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "658646b21e0b72f7866c7038ab086d3d5e1cd6271f060fd37defb241949d0582"
dependencies = [
"digest",
]
[[package]]
name = "md5-dist"
version = "0.1.0"
dependencies = [
"md-5",
]
[[package]]
name = "typenum"
version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcf81ac59edc17cc8697ff311e8f5ef2d99fcbd9817b34cec66f90b6c3dfd987"
[[package]]
name = "version_check"
version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"

View File

@ -0,0 +1,9 @@
[package]
name = "md5-dist"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
md-5 = "0.10.1"

256
codes/md5-dist/countmap.txt Normal file
View File

@ -0,0 +1,256 @@
00: 16775418
01: 16784471
02: 16782205
03: 16777355
04: 16781626
05: 16775143
06: 16773409
07: 16783120
08: 16780738
09: 16776141
0a: 16780852
0b: 16779188
0c: 16769188
0d: 16778425
0e: 16782101
0f: 16773329
10: 16775877
11: 16773625
12: 16782333
13: 16779312
14: 16778791
15: 16772556
16: 16776048
17: 16776984
18: 16774961
19: 16772505
1a: 16772749
1b: 16784280
1c: 16776018
1d: 16780113
1e: 16775174
1f: 16775598
20: 16775411
21: 16775281
22: 16781508
23: 16782609
24: 16774699
25: 16774058
26: 16780100
27: 16778931
28: 16785083
29: 16774624
2a: 16769029
2b: 16769432
2c: 16779676
2d: 16777681
2e: 16775010
2f: 16779064
30: 16770980
31: 16775936
32: 16780708
33: 16774265
34: 16775831
35: 16781156
36: 16778539
37: 16774660
38: 16775533
39: 16774402
3a: 16775884
3b: 16776471
3c: 16782541
3d: 16770076
3e: 16784726
3f: 16782120
40: 16783936
41: 16776491
42: 16769981
43: 16779010
44: 16780694
45: 16774333
46: 16771538
47: 16773219
48: 16781715
49: 16777370
4a: 16778144
4b: 16778755
4c: 16773066
4d: 16775350
4e: 16779655
4f: 16779264
50: 16772744
51: 16776626
52: 16779230
53: 16783053
54: 16780042
55: 16778545
56: 16778630
57: 16779584
58: 16786895
59: 16781375
5a: 16778843
5b: 16775092
5c: 16779425
5d: 16772850
5e: 16783451
5f: 16779147
60: 16786462
61: 16777634
62: 16777892
63: 16769887
64: 16774137
65: 16778514
66: 16778853
67: 16771229
68: 16785638
69: 16779226
6a: 16773444
6b: 16782595
6c: 16781253
6d: 16776543
6e: 16775492
6f: 16785094
70: 16774113
71: 16772065
72: 16775977
73: 16778131
74: 16767229
75: 16779234
76: 16778190
77: 16778508
78: 16774681
79: 16780882
7a: 16772074
7b: 16774136
7c: 16778663
7d: 16769883
7e: 16777787
7f: 16777442
80: 16770867
81: 16780600
82: 16781874
83: 16783000
84: 16780492
85: 16777010
86: 16771862
87: 16771841
88: 16775756
89: 16780580
8a: 16788426
8b: 16785673
8c: 16776136
8d: 16774636
8e: 16774131
8f: 16776528
90: 16778622
91: 16779850
92: 16774853
93: 16786692
94: 16785159
95: 16786085
96: 16779068
97: 16778402
98: 16770266
99: 16775790
9a: 16772544
9b: 16780165
9c: 16771988
9d: 16774073
9e: 16775503
9f: 16775211
a0: 16778822
a1: 16770790
a2: 16772720
a3: 16782384
a4: 16777523
a5: 16776009
a6: 16776404
a7: 16780201
a8: 16786679
a9: 16782734
aa: 16781532
ab: 16774293
ac: 16778811
ad: 16773288
ae: 16777402
af: 16780705
b0: 16771373
b1: 16772570
b2: 16776390
b3: 16769489
b4: 16780751
b5: 16780086
b6: 16773617
b7: 16779288
b8: 16772726
b9: 16773539
ba: 16772098
bb: 16771896
bc: 16770747
bd: 16779381
be: 16776967
bf: 16780660
c0: 16774753
c1: 16777763
c2: 16778860
c3: 16774258
c4: 16769455
c5: 16781673
c6: 16777371
c7: 16779255
c8: 16780965
c9: 16774925
ca: 16773390
cb: 16766367
cc: 16779007
cd: 16774152
ce: 16779393
cf: 16775566
d0: 16777541
d1: 16775665
d2: 16767130
d3: 16777702
d4: 16774089
d5: 16778767
d6: 16777777
d7: 16778717
d8: 16774044
d9: 16769863
da: 16775188
db: 16780781
dc: 16780489
dd: 16774530
de: 16785037
df: 16780348
e0: 16781971
e1: 16785674
e2: 16775852
e3: 16772744
e4: 16777170
e5: 16783475
e6: 16778994
e7: 16775288
e8: 16771541
e9: 16776289
ea: 16778383
eb: 16780526
ec: 16780465
ed: 16778746
ee: 16770117
ef: 16775146
f0: 16775217
f1: 16777215
f2: 16776901
f3: 16779481
f4: 16778597
f5: 16776573
f6: 16773471
f7: 16778843
f8: 16775212
f9: 16776428
fa: 16771828
fb: 16771978
fc: 16781247
fd: 16778215
fe: 16778741
ff: 16777991

View File

@ -0,0 +1,28 @@
use md5::{Digest, Md5};
use std::{
collections::HashMap,
fs::File,
io::{BufWriter, Write},
};
fn main() -> std::io::Result<()> {
let mut map = HashMap::new();
for i in 0u32..=std::u32::MAX {
let mut hasher = Md5::new();
hasher.update(i.to_le_bytes());
let out = hasher.finalize();
let key = out[out.len() - 1];
*map.entry(key).or_insert(0) += 1;
if i % 100_000 == 0 {
println!("{i}");
}
}
let mut count_list = map.into_iter().collect::<Vec<_>>();
count_list.sort();
let file = File::create("./countmap.txt")?;
let mut writer = BufWriter::new(file);
for (k, v) in count_list {
writeln!(writer, "{:02x}: {}", k, v)?;
}
Ok(())
}

View File

@ -0,0 +1,21 @@
use md5::{Digest, Md5};
use std::{
fs::File,
io::{BufWriter, Write},
};
fn main() -> std::io::Result<()> {
let file = File::create("./hashes.txt")?;
let mut writer = BufWriter::new(file);
for i in 0u32..=std::u32::MAX {
let mut hasher = Md5::new();
// let mut out = [0; 16];
hasher.update(i.to_le_bytes());
let out = hasher.finalize();
writeln!(writer, "{:02x}", out[out.len() - 1])?;
if i % 100_000 == 0 {
println!("{i}");
}
}
Ok(())
}

1
codes/md5dist-plot/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.venv

View File

@ -0,0 +1,256 @@
00: 16775418
01: 16784471
02: 16782205
03: 16777355
04: 16781626
05: 16775143
06: 16773409
07: 16783120
08: 16780738
09: 16776141
0a: 16780852
0b: 16779188
0c: 16769188
0d: 16778425
0e: 16782101
0f: 16773329
10: 16775877
11: 16773625
12: 16782333
13: 16779312
14: 16778791
15: 16772556
16: 16776048
17: 16776984
18: 16774961
19: 16772505
1a: 16772749
1b: 16784280
1c: 16776018
1d: 16780113
1e: 16775174
1f: 16775598
20: 16775411
21: 16775281
22: 16781508
23: 16782609
24: 16774699
25: 16774058
26: 16780100
27: 16778931
28: 16785083
29: 16774624
2a: 16769029
2b: 16769432
2c: 16779676
2d: 16777681
2e: 16775010
2f: 16779064
30: 16770980
31: 16775936
32: 16780708
33: 16774265
34: 16775831
35: 16781156
36: 16778539
37: 16774660
38: 16775533
39: 16774402
3a: 16775884
3b: 16776471
3c: 16782541
3d: 16770076
3e: 16784726
3f: 16782120
40: 16783936
41: 16776491
42: 16769981
43: 16779010
44: 16780694
45: 16774333
46: 16771538
47: 16773219
48: 16781715
49: 16777370
4a: 16778144
4b: 16778755
4c: 16773066
4d: 16775350
4e: 16779655
4f: 16779264
50: 16772744
51: 16776626
52: 16779230
53: 16783053
54: 16780042
55: 16778545
56: 16778630
57: 16779584
58: 16786895
59: 16781375
5a: 16778843
5b: 16775092
5c: 16779425
5d: 16772850
5e: 16783451
5f: 16779147
60: 16786462
61: 16777634
62: 16777892
63: 16769887
64: 16774137
65: 16778514
66: 16778853
67: 16771229
68: 16785638
69: 16779226
6a: 16773444
6b: 16782595
6c: 16781253
6d: 16776543
6e: 16775492
6f: 16785094
70: 16774113
71: 16772065
72: 16775977
73: 16778131
74: 16767229
75: 16779234
76: 16778190
77: 16778508
78: 16774681
79: 16780882
7a: 16772074
7b: 16774136
7c: 16778663
7d: 16769883
7e: 16777787
7f: 16777442
80: 16770867
81: 16780600
82: 16781874
83: 16783000
84: 16780492
85: 16777010
86: 16771862
87: 16771841
88: 16775756
89: 16780580
8a: 16788426
8b: 16785673
8c: 16776136
8d: 16774636
8e: 16774131
8f: 16776528
90: 16778622
91: 16779850
92: 16774853
93: 16786692
94: 16785159
95: 16786085
96: 16779068
97: 16778402
98: 16770266
99: 16775790
9a: 16772544
9b: 16780165
9c: 16771988
9d: 16774073
9e: 16775503
9f: 16775211
a0: 16778822
a1: 16770790
a2: 16772720
a3: 16782384
a4: 16777523
a5: 16776009
a6: 16776404
a7: 16780201
a8: 16786679
a9: 16782734
aa: 16781532
ab: 16774293
ac: 16778811
ad: 16773288
ae: 16777402
af: 16780705
b0: 16771373
b1: 16772570
b2: 16776390
b3: 16769489
b4: 16780751
b5: 16780086
b6: 16773617
b7: 16779288
b8: 16772726
b9: 16773539
ba: 16772098
bb: 16771896
bc: 16770747
bd: 16779381
be: 16776967
bf: 16780660
c0: 16774753
c1: 16777763
c2: 16778860
c3: 16774258
c4: 16769455
c5: 16781673
c6: 16777371
c7: 16779255
c8: 16780965
c9: 16774925
ca: 16773390
cb: 16766367
cc: 16779007
cd: 16774152
ce: 16779393
cf: 16775566
d0: 16777541
d1: 16775665
d2: 16767130
d3: 16777702
d4: 16774089
d5: 16778767
d6: 16777777
d7: 16778717
d8: 16774044
d9: 16769863
da: 16775188
db: 16780781
dc: 16780489
dd: 16774530
de: 16785037
df: 16780348
e0: 16781971
e1: 16785674
e2: 16775852
e3: 16772744
e4: 16777170
e5: 16783475
e6: 16778994
e7: 16775288
e8: 16771541
e9: 16776289
ea: 16778383
eb: 16780526
ec: 16780465
ed: 16778746
ee: 16770117
ef: 16775146
f0: 16775217
f1: 16777215
f2: 16776901
f3: 16779481
f4: 16778597
f5: 16776573
f6: 16773471
f7: 16778843
f8: 16775212
f9: 16776428
fa: 16771828
fb: 16771978
fc: 16781247
fd: 16778215
fe: 16778741
ff: 16777991

BIN
codes/md5dist-plot/dist.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 18 KiB

46
codes/md5dist-plot/dist.py Executable file
View File

@ -0,0 +1,46 @@
#!/usr/bin/env python3
from collections import defaultdict
import matplotlib.pyplot as plt
def main():
with open('./countmap.txt', 'r') as f:
countmap = {}
keys = []
values = []
for line in f:
split = line.split(': ')
prefix = split[0].strip()
key = int(prefix, 16)
value = int(split[1].strip())
countmap[key] = value
keys.append(key)
values.append(value)
plt.bar(keys, values)
plt.title('Distribution of lowest byte of hashed IP addresses')
plt.savefig('dist.png')
plt.show()
# with open('./hashes.txt', 'r') as f:
# # countmap = defaultdict(lambda: 0)
# ints = set()
# data = []
# for line in f.readlines():
# # line = line.strip()
# # prefix = line[len(line) - 2:]
# prefix = line.strip()
# ints.add(prefix)
# # countmap[prefix] = countmap[prefix] + 1
# data.append(int(prefix, 16))
# plt.hist(data, bins=len(ints))
# plt.title('Distribution of lowest byte of hashed IP addresses')
# plt.savefig('dist.png')
# plt.show()
# # print(countmap)
if __name__ == '__main__':
main()

View File

@ -0,0 +1 @@
matplotlib==3.5.1