-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
100 lines (89 loc) · 2.58 KB
/
main.go
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
package main
/*
Tasmota Fingerprint Generator - Used to calculate TLS fingerprints
expected by Tasmota firmware
Copyright (c) 2019 Issac Goldstand <[email protected]>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
import (
"crypto/rsa"
"crypto/sha1"
"crypto/x509"
"encoding/binary"
"encoding/pem"
"fmt"
"io/ioutil"
"os"
)
func reverse(ar []byte) []byte {
for i := 0; i < len(ar)/2; i++ {
j := len(ar) - i - 1
ar[i], ar[j] = ar[j], ar[i]
}
return ar
}
func main() {
var data []byte
var err error
if len(os.Args) > 1 && os.Args[1] != "-" {
data, err = ioutil.ReadFile(os.Args[1])
if err != nil {
panic(err)
}
} else {
info, err := os.Stdin.Stat()
if err != nil {
panic(err)
}
if info.Mode()&os.ModeCharDevice != 0 {
panic("Missing input on STDIN")
}
data, err = ioutil.ReadAll(os.Stdin)
}
block, _ := pem.Decode(data)
if block == nil {
panic("Error reading PEM data")
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
panic(err)
}
pub := cert.PublicKey.(*rsa.PublicKey)
buf := make([]byte, binary.MaxVarintLen64)
binary.LittleEndian.PutUint64(buf, uint64(pub.E))
buf = reverse(buf)
start := 0
for start < len(buf) && buf[start] == 0 {
start++
}
e_bytes := buf[start:]
e_length := make([]byte, 4)
e_length[0] = byte(len(e_bytes) >> 24 & 255)
e_length[1] = byte(len(e_bytes) >> 16 & 255)
e_length[2] = byte(len(e_bytes) >> 8 & 255)
e_length[3] = byte(len(e_bytes) >> 0 & 255)
n_bytes := pub.N.Bytes()
n_length := make([]byte, 4)
n_length[0] = byte(len(n_bytes) >> 24 & 255)
n_length[1] = byte(len(n_bytes) >> 16 & 255)
n_length[2] = byte(len(n_bytes) >> 8 & 255)
n_length[3] = byte(len(n_bytes) >> 0 & 255)
ctx := sha1.New()
ctx.Write([]byte("\000\000\000\007")) // length of "ssh-rsa"
ctx.Write([]byte("ssh-rsa"))
ctx.Write(e_length)
ctx.Write(e_bytes)
ctx.Write(n_length)
ctx.Write(n_bytes)
fmt.Printf("% X (Tasmota v8.4.0+)\n", ctx.Sum(nil))
}