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)
| 23 | // If the size of "chars" is longer than "size", |
| 24 | // "chars" are truncated to "size". |
| 25 | func 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 |
| 52 | func (p *Polybius) Encrypt(text string) (string, error) { |
no outgoing calls