MCPcopy Index your code
hub / github.com/TheAlgorithms/Go / NewPolybius

Function NewPolybius

cipher/polybius/polybius.go:25–49  ·  view source on GitHub ↗

NewPolybius returns a pointer to object of Polybius. If the size of "chars" is longer than "size", "chars" are truncated to "size".

(key string, size int, chars string)

Source from the content-addressed store, hash-verified

23// If the size of "chars" is longer than "size",
24// "chars" are truncated to "size".
25func NewPolybius(key string, size int, chars string) (*Polybius, error) {
26 if size < 0 {
27 return nil, fmt.Errorf("provided size %d cannot be negative", size)
28 }
29 key = strings.ToUpper(key)
30 if size > len(chars) {
31 return nil, fmt.Errorf("provided size %d is too small to use to slice string %q of len %d", size, chars, len(chars))
32 }
33 for _, r := range chars {
34 if (r < 'a' || r > 'z') && (r < 'A' || r > 'Z') {
35 return nil, fmt.Errorf("provided string %q should only contain latin characters", chars)
36 }
37 }
38 chars = strings.ToUpper(chars)[:size]
39 for i, r := range chars {
40 if strings.ContainsRune(chars[i+1:], r) {
41 return nil, fmt.Errorf("%q contains same character %q", chars[i+1:], r)
42 }
43 }
44
45 if len(key) != size*size {
46 return nil, fmt.Errorf("len(key): %d must be as long as size squared: %d", len(key), size*size)
47 }
48 return &Polybius{size, chars, key}, nil
49}
50
51// Encrypt encrypts with polybius encryption
52func (p *Polybius) Encrypt(text string) (string, error) {

Callers 5

ExampleNewPolybiusFunction · 0.85
TestNewPolybiusFunction · 0.85
TestPolybiusEncryptFunction · 0.85
TestPolybiusDecryptFunction · 0.85
FuzzPolybiusFunction · 0.85

Calls

no outgoing calls

Tested by 5

ExampleNewPolybiusFunction · 0.68
TestNewPolybiusFunction · 0.68
TestPolybiusEncryptFunction · 0.68
TestPolybiusDecryptFunction · 0.68
FuzzPolybiusFunction · 0.68