Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
Go/cipher/polybius/polybius_test.go at master · TheAlgorithms/Go · GitHub
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Events
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
TheAlgorithms
Go
Repository navigation
Code
Issues
6
(6)
Pull requests
5
(5)
Discussions
Actions
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
Go
/
cipher
/
polybius
/
polybius_test.go
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
190 lines (181 loc) · 5 KB
master
Breadcrumbs
Go
/
cipher
/
polybius
/
polybius_test.go
Copy path
Top
File metadata and controls
Code
Blame
190 lines (181 loc) · 5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package
polybius
import
(
"fmt"
"log"
"strings"
"testing"
)
func
ExampleNewPolybius
() {
// initialize
const
(
plainText
=
"HogeFugaPiyoSpam"
size
=
5
characters
=
"HogeF"
key
=
"abcdefghijklmnopqrstuvwxy"
)
p
,
err
:=
NewPolybius
(
key
,
size
,
characters
)
if
err
!=
nil
{
log
.
Fatalf
(
"failed NewPolybius: %v"
,
err
)
}
encryptedText
,
err
:=
p
.
Encrypt
(
plainText
)
if
err
!=
nil
{
log
.
Fatalf
(
"failed Encrypt: %v"
,
err
)
}
fmt
.
Printf
(
"Encrypt=> plainText: %s, encryptedText: %s
\n
"
,
plainText
,
encryptedText
)
decryptedText
,
err
:=
p
.
Decrypt
(
encryptedText
)
if
err
!=
nil
{
log
.
Fatalf
(
"failed Decrypt: %v"
,
err
)
}
fmt
.
Printf
(
"Decrypt=> encryptedText: %s, decryptedText: %s
\n
"
,
encryptedText
,
decryptedText
)
// Output:
// Encrypt=> plainText: HogeFugaPiyoSpam, encryptedText: OGGFOOHFOHFHOOHHEHOEFFGFEEEHHHGG
// Decrypt=> encryptedText: OGGFOOHFOHFHOOHHEHOEFFGFEEEHHHGG, decryptedText: HOGEFUGAPIYOSPAM
}
func
TestNewPolybius
(
t
*
testing.
T
) {
t
.
Parallel
()
cases
:=
[]
struct
{
name
string
size
int
characters
string
key
string
wantErr
string
}{
{
name
:
"correct initialization"
,
size
:
5
,
characters
:
"HogeF"
,
key
:
"abcdefghijklmnopqrstuvwxy"
,
wantErr
:
""
,
},
{
name
:
"truncate characters"
,
size
:
5
,
characters
:
"HogeFuga"
,
key
:
"abcdefghijklmnopqrstuvwxy"
,
wantErr
:
""
,
},
{
name
:
"invalid key"
,
size
:
5
,
characters
:
"HogeFuga"
,
key
:
"abcdefghi"
,
wantErr
:
"len(key): 9 must be as long as size squared: 25"
,
},
{
name
:
"invalid characters"
,
size
:
5
,
characters
:
"HogeH"
,
key
:
"abcdefghijklmnopqrstuvwxy"
,
wantErr
:
"
\"
OGEH
\"
contains same character 'H'"
,
},
}
for
_
,
tc
:=
range
cases
{
t
.
Run
(
tc
.
name
,
func
(
t
*
testing.
T
) {
_
,
err
:=
NewPolybius
(
tc
.
key
,
tc
.
size
,
tc
.
characters
)
if
err
!=
nil
&&
err
.
Error
()
!=
tc
.
wantErr
{
t
.
Errorf
(
"failed NewPolybius: %v"
,
err
)
}
})
}
}
func
TestPolybiusEncrypt
(
t
*
testing.
T
) {
t
.
Parallel
()
cases
:=
[]
struct
{
name
string
text
string
want
string
}{
{
name
:
"correct encryption"
,
text
:
"HogeFugaPiyoSpam"
,
want
:
"OGGFOOHFOHFHOOHHEHOEFFGFEEEHHHGG"
,
},
{
name
:
"invalid encryption"
,
text
:
"hogz"
,
want
:
"failed encipher: 'Z' does not exist in keys"
,
},
}
// initialize
const
(
size
=
5
characters
=
"HogeF"
key
=
"abcdefghijklmnopqrstuvwxy"
)
p
,
err
:=
NewPolybius
(
key
,
size
,
characters
)
if
err
!=
nil
{
t
.
Fatalf
(
"failed NewPolybius: %v"
,
err
)
}
for
_
,
tc
:=
range
cases
{
t
.
Run
(
tc
.
name
,
func
(
t
*
testing.
T
) {
encrypted
,
err
:=
p
.
Encrypt
(
tc
.
text
)
if
err
!=
nil
{
if
err
.
Error
()
!=
tc
.
want
{
t
.
Errorf
(
"failed Encrypt: %v"
,
err
)
}
}
else
if
encrypted
!=
tc
.
want
{
t
.
Errorf
(
"Encrypt: %v, want: %v"
,
encrypted
,
tc
.
want
)
}
})
}
}
func
TestPolybiusDecrypt
(
t
*
testing.
T
) {
t
.
Parallel
()
cases
:=
[]
struct
{
name
string
text
string
want
string
}{
{
name
:
"correct decryption"
,
text
:
"OGGFOOHFOHFHOOHHEHOEFFGFEEEHHHGG"
,
want
:
"HOGEFUGAPIYOSPAM"
,
},
{
name
:
"invalid decryption(position of even number)"
,
text
:
"hogz"
,
want
:
"failed decipher: Z does not exist in characters"
,
},
{
name
:
"invalid decryption(position of odd number)"
,
text
:
"hode"
,
want
:
"failed decipher: D does not exist in characters"
,
},
{
name
:
"invalid text size which is odd"
,
text
:
"hog"
,
want
:
"failed decipher: the size of
\"
chars
\"
must be even"
,
},
}
// initialize
const
(
size
=
5
characters
=
"HogeF"
key
=
"abcdefghijklmnopqrstuvwxy"
)
p
,
err
:=
NewPolybius
(
key
,
size
,
characters
)
if
err
!=
nil
{
t
.
Fatalf
(
"failed NewPolybius: %v"
,
err
)
}
for
_
,
tc
:=
range
cases
{
t
.
Run
(
tc
.
name
,
func
(
t
*
testing.
T
) {
encrypted
,
err
:=
p
.
Decrypt
(
tc
.
text
)
if
err
!=
nil
{
if
err
.
Error
()
!=
tc
.
want
{
t
.
Errorf
(
"failed Encrypt: %v"
,
err
)
}
}
else
if
encrypted
!=
tc
.
want
{
t
.
Errorf
(
"Encrypt: %v, want: %v"
,
encrypted
,
tc
.
want
)
}
})
}
}
func
FuzzPolybius
(
f
*
testing.
F
) {
const
(
size
=
5
characters
=
"HogeF"
key
=
"abcdefghijklmnopqrstuvwxy"
)
f
.
Add
(
size
,
characters
,
key
)
f
.
Fuzz
(
func
(
t
*
testing.
T
,
size
int
,
characters
,
key
string
) {
p
,
err
:=
NewPolybius
(
key
,
size
,
characters
)
switch
{
case
err
==
nil
:
case
strings
.
Contains
(
err
.
Error
(),
"cannot be negative"
),
strings
.
Contains
(
err
.
Error
(),
"is too small"
),
strings
.
Contains
(
err
.
Error
(),
"should only contain latin characters"
),
strings
.
Contains
(
err
.
Error
(),
"contains same character"
),
strings
.
Contains
(
err
.
Error
(),
"must be as long as size squared"
):
return
default
:
t
.
Fatalf
(
"unexpected error when creating a new polybius variable: %v"
,
err
)
}
encrypted
,
err
:=
p
.
Encrypt
(
characters
)
switch
{
case
err
==
nil
:
case
strings
.
Contains
(
err
.
Error
(),
"does not exist in keys"
):
return
default
:
t
.
Fatalf
(
"unexpected error during encryption: %v"
,
err
)
}
decrypted
,
err
:=
p
.
Decrypt
(
encrypted
)
if
err
!=
nil
{
t
.
Fatalf
(
"unexpected error during decryption: %v"
,
err
)
}
if
decrypted
!=
strings
.
ToUpper
(
characters
) {
t
.
Errorf
(
"Expecting output to match with %q but was %q"
,
characters
,
decrypted
)
}
})
}
You can’t perform that action at this time.