Skip to content

Commit 416e304

Browse files
committed
init
1 parent 62b2a5c commit 416e304

15 files changed

+528
-0
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.idea
2+
test.jpg

README.md

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# 猪圈密码
2+
3+
# 一、猪圈密码介绍
4+
5+
猪圈密码共济会密码解密,朱高密码解密,PigPen_chiper密码
6+
7+
# 二、安装
8+
```bash
9+
go get -u github.com/cryptography-research-lab/go-pigpen-cipher
10+
```
11+
# 三、API使用示例
12+
13+
```go
14+
15+
```
16+
17+
# 四、TODO
18+
19+
20+
21+
22+
23+
24+
字体文件来自: http://www.metools.info/code/c89.html,感谢站长!
25+
26+
27+
28+
29+
30+
src: url("/skin/css/pigpass/pigpass-font.woff2") format("woff2"),
31+
url("/skin/css/pigpass/pigpass-font.woff") format("woff"),
32+
url("") format("truetype"),
33+
url("") format("embedded-opentype"),
34+
url("") format("svg");
35+
font-weight:normal;
36+
37+
38+
39+

data/data.go

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package data
2+
3+
import _ "embed"
4+
5+
//go:embed pigpen-font.woff
6+
var PigpenFontWoff []byte
7+
8+
//go:embed pigpen-font.woff2
9+
var PigpenFontWoff2 []byte
10+
11+
//go:embed pigpen-font.ttf
12+
var PigpenFontTTF []byte
13+
14+
//go:embed pigpen-font.svg
15+
var PigpenFontSVG []byte
16+
17+
//go:embed pigpen-font.eot
18+
var PigpenFontEot []byte

data/pigpen-font.eot

13.9 KB
Binary file not shown.

data/pigpen-font.svg

+250
Loading

data/pigpen-font.ttf

13.7 KB
Binary file not shown.

data/pigpen-font.woff

3.26 KB
Binary file not shown.

data/pigpen-font.woff2

1.8 KB
Binary file not shown.

errors.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package pigpen_cipher
2+
3+
import "errors"
4+
5+
// ErrPlaintextMustIsLetters 要加密的明文必须都是英文字母
6+
var ErrPlaintextMustIsLetters = errors.New("plaintext must is letters")

examples/basic_usage.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
pigpen_cipher "github.com/cryptography-research-lab/go-pigpen-cipher"
6+
"os"
7+
)
8+
9+
func main() {
10+
11+
// 把明文加密为图片
12+
imageBytes, err := pigpen_cipher.Encrypt("helloworld", pigpen_cipher.NewOptions().WithFontSize(80))
13+
if err != nil {
14+
fmt.Println("加密失败: " + err.Error())
15+
return
16+
}
17+
18+
// 把图片保存到本地查看
19+
err = os.WriteFile("test.jpg", imageBytes, os.ModePerm)
20+
if err != nil {
21+
fmt.Println("图片保存失败: " + err.Error())
22+
return
23+
}
24+
fmt.Println("图片保存成功!")
25+
26+
}

go.mod

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
module github.com/cryptography-research-lab/go-pigpen-cipher
2+
3+
go 1.18
4+
5+
require (
6+
github.com/davecgh/go-spew v1.1.1 // indirect
7+
github.com/golang-infrastructure/go-variable-parameter v0.0.3 // indirect
8+
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
github.com/stretchr/objx v0.5.0 // indirect
11+
github.com/stretchr/testify v1.8.1 // indirect
12+
golang.org/x/image v0.3.0 // indirect
13+
gopkg.in/yaml.v3 v3.0.1 // indirect
14+
)

go.sum

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/golang-infrastructure/go-variable-parameter v0.0.3 h1:+roJv+T/krkl5YMZtCXzdgDSP0RDWK0W1V2lTkpeDNA=
5+
github.com/golang-infrastructure/go-variable-parameter v0.0.3/go.mod h1:qEnCqgG04+EpGWqRXaNpKhu4JsP6xcT85WpJyS83Dcw=
6+
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
7+
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
8+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
9+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
10+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
11+
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
12+
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
13+
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
14+
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
15+
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
16+
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
17+
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
18+
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
19+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
20+
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
21+
golang.org/x/image v0.3.0 h1:HTDXbdK9bjfSWkPzDJIw89W8CAtfFGduujWs33NLLsg=
22+
golang.org/x/image v0.3.0/go.mod h1:fXd9211C/0VTlYuAcOhW8dY/RtEJqODXOWBDpmYBf+A=
23+
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
24+
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
25+
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
26+
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
27+
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
28+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
29+
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
30+
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
31+
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
32+
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
33+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
34+
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
35+
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
36+
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
37+
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
38+
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
39+
golang.org/x/text v0.6.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
40+
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
41+
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
42+
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
43+
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
44+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
45+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
46+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
47+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

pigpen_cipher.go

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package pigpen_cipher
2+
3+
import (
4+
"bytes"
5+
"github.com/cryptography-research-lab/go-pigpen-cipher/data"
6+
variable_parameter "github.com/golang-infrastructure/go-variable-parameter"
7+
"github.com/golang/freetype"
8+
"golang.org/x/image/draw"
9+
"image"
10+
"image/png"
11+
)
12+
13+
// ------------------------------------------------ ---------------------------------------------------------------------
14+
15+
// Option 加密时的各种选项
16+
type Option struct {
17+
18+
// 每个字体有多大
19+
FontSize int
20+
21+
// 多少列一换行
22+
ColumnsWidth int
23+
}
24+
25+
func NewOptions() *Option {
26+
return &Option{
27+
FontSize: 50,
28+
ColumnsWidth: 80,
29+
}
30+
}
31+
32+
func (x *Option) WithFontSize(fontSize int) *Option {
33+
x.FontSize = fontSize
34+
return x
35+
}
36+
37+
func (x *Option) WithColumnsWidth(columnsWidth int) *Option {
38+
x.ColumnsWidth = columnsWidth
39+
return x
40+
}
41+
42+
// ------------------------------------------------ ---------------------------------------------------------------------
43+
44+
// Encrypt 对字符加密,返回加密后的图片
45+
func Encrypt(plaintext string, options ...*Option) ([]byte, error) {
46+
47+
// 检查输入的明文,必须都是英文字母
48+
for _, c := range plaintext {
49+
isOk := (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')
50+
if !isOk {
51+
return nil, ErrPlaintextMustIsLetters
52+
}
53+
}
54+
55+
// 设置默认参数
56+
options = variable_parameter.SetDefaultParamByFunc(options, NewOptions)
57+
58+
width := len(plaintext)*options[0].FontSize + 10
59+
height := ((len(plaintext)+options[0].ColumnsWidth-1)/options[0].ColumnsWidth)*options[0].FontSize + 10
60+
background := image.NewRGBA(image.Rect(0, 0, width, height))
61+
draw.Draw(background, background.Bounds(), image.White, image.Point{X: 0, Y: 0}, draw.Src)
62+
context, err := GetCharacterContext()
63+
if err != nil {
64+
return nil, err
65+
}
66+
context.SetClip(background.Bounds())
67+
context.SetDst(background)
68+
context.SetSrc(image.Black)
69+
context.SetFontSize(float64(options[0].FontSize))
70+
pt := freetype.Pt(0, options[0].FontSize/2+1-3+int(context.PointToFixed(26)>>6))
71+
_, err = context.DrawString(plaintext, pt)
72+
if err != nil {
73+
return nil, err
74+
}
75+
buffer := &bytes.Buffer{}
76+
err = png.Encode(buffer, background)
77+
if err != nil {
78+
return nil, err
79+
}
80+
return buffer.Bytes(), nil
81+
}
82+
83+
// Decrypt 对猪圈密码解密
84+
// TODO 待实现,还没想好咋实现图形识别
85+
func Decrypt() {
86+
87+
}
88+
89+
// GetCharacterContext 读取字体文件
90+
func GetCharacterContext() (*freetype.Context, error) {
91+
f, err := freetype.ParseFont(data.PigpenFontTTF)
92+
if err != nil {
93+
return nil, err
94+
}
95+
c := freetype.NewContext()
96+
c.SetFont(f)
97+
c.SetDPI(72)
98+
c.SetFont(f)
99+
c.SetFontSize(26)
100+
return c, nil
101+
}

pigpen_cipher_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package pigpen_cipher
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"os"
6+
"testing"
7+
)
8+
9+
func TestEncrypt(t *testing.T) {
10+
imageBytes, err := Encrypt("test", NewOptions().WithFontSize(80))
11+
assert.Nil(t, err)
12+
13+
err = os.WriteFile("test.jpg", imageBytes, os.ModePerm)
14+
assert.Nil(t, err)
15+
}

table.go

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package pigpen_cipher
2+
3+
type Table []rune
4+
5+
func F() {
6+
7+
}
8+
9+
// 草,怎么表示这些图形啊...
10+
var DefaultTable Table = []rune{}

0 commit comments

Comments
 (0)