{"id":645,"date":"2020-05-11T10:14:20","date_gmt":"2020-05-11T10:14:20","guid":{"rendered":"https:\/\/blog.samarthya.me\/wps\/?p=645"},"modified":"2020-05-11T10:16:16","modified_gmt":"2020-05-11T10:16:16","slug":"golang-simplecipher","status":"publish","type":"post","link":"https:\/\/blog.samarthya.me\/wps\/2020\/05\/11\/golang-simplecipher\/","title":{"rendered":"GoLang: SimpleCipher"},"content":{"rendered":"<p>Hello back again, Golang is just a new syntax for me. Even though behind the scenes they developers have simplified many things, for a consumer like me writing a program is more about learning the new syntax and getting familiarized with the control and stack.<\/p>\n<p>In my constant quest to try something different, today I try and solve the simple cipher which by definition is a text replacement algorithm that makes the text a little difficult to break while reading with naked eyes.<\/p>\n<p><a href=\"https:\/\/github.com\/samarthya\/goexercism\/tree\/master\/simple-cipher\">SimpleCipher<\/a>: Code available in GitHub<\/p>\n<p><img decoding=\"async\" class=\"aligncenter size-medium\" src=\"https:\/\/camo.githubusercontent.com\/17a2d0ae02743554139d4045a62cb943a40c8e51\/68747470733a2f2f75706c6f61642e77696b696d656469612e6f72672f77696b6970656469612f636f6d6d6f6e732f7468756d622f342f34612f4361657361725f6369706865725f6c6566745f73686966745f6f665f332e7376672f33323070782d4361657361725f6369706865725f6c6566745f73686966745f6f665f332e7376672e706e67\" width=\"320\" height=\"135\"><\/p>\n<h2>Version 1.0<\/h2>\n<pre>The first version was a basic logic writing which allowed to check for the code and decode capability.<\/pre>\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Encrypt struct\ntype Encrypt struct {\n\tdistance int\n\tkey      string\n}\n\n\/\/ NewCaesar Object that implements the interface Cipher\nfunc NewCaesar() Cipher {\n\treturn Encrypt{distance: 3, key: \"\"}\n}\n\n\/\/ NewShift Object that implements the interface Cipher\nfunc NewShift(distance int) Cipher {\n\t\/\/ Corner case when of 0, 26, -26\n\tif distance &lt; -26 || distance > 26 || distance == 0 {\n\t\treturn nil\n\t}\n\treturn Encrypt{distance, \"\"}\n}\n\n\/\/ NewVigenere Object that implements the interface Cipher\nfunc NewVigenere(key string) Cipher {\n\n\tif key == \"\" || checkKeyInvalid(key) {\n\t\treturn nil\n\t}\n\treturn Encrypt{0, key}\n}\n\n\/\/ checkAllAs Checks for all A's in the key.\nfunc checkKeyInvalid(s string) (f bool) {\n\n\t\/\/ If any invalid character matches\n\tif matched, _ := regexp.MatchString(`&#91;^a-z]`, s); matched {\n\t\treturn true\n\t}\n\n\tfor _, m := range s {\n\t\tswitch m {\n\t\tcase 'a':\n\t\t\tf = true\n\t\tdefault:\n\t\t\tf = false\n\t\t\treturn\n\t\t}\n\t}\n\treturn\n}<\/code><\/pre>\n\n\n\n<p>I also wrote a utility to remove all invalid character basically all the chars that were not either of A-Z or a-z.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ removeAllOtherCharcterBeforeEncoding replace all the other elements\nfunc removeAllOtherCharcterBeforeEncoding(s string) (o string) {\n\to = RegularExp.ReplaceAllString(s, \"\")\n\tlog.Printf(\" String: %s\", o)\n\to = strings.ToLower(o)\n\treturn\n}\n\nvar RegularExp = regexp.MustCompile(`&#91;^A-Za-z]`)<\/code><\/pre>\n\n\n\n<p>The basic logic of transforming the input string to encoded string was written in these two functions below<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>func transformWithString(s string, e string, flag bool) string {\n\tvar sb strings.Builder\n\n\tem := &#91;]rune(e)\n\n\tfor i, sm := range s {\n\t\t\/\/ The key has to be repeated\n\t\tvar d rune = 0\n\n\t\tif i &lt; len(em) {\n\t\t\td = em&#91;i] - 'a'\n\t\t} else {\n\t\t\td = em&#91;(i%len(em))] - 'a'\n\t\t}\n\n\t\tif flag {\n\t\t\tswitch {\n\t\t\tcase sm >= 'a' &amp;&amp; sm &lt;= 'z':\n\t\t\t\tfmt.Fprintf(&amp;sb, \"%s\", string('a'+(sm-'a'+d)%26))\n\t\t\t}\n\t\t} else {\n\t\t\tswitch {\n\t\t\tcase sm >= 'a' &amp;&amp; sm &lt;= 'z':\n\t\t\t\tdiff := ((sm - 'a' - d) % 26)\n\t\t\t\tnewChar := \"\"\n\n\t\t\t\tif diff &lt; 0 {\n\t\t\t\t\tnewChar = string('z' + diff + 1)\n\t\t\t\t} else {\n\t\t\t\t\tnewChar = string('a' + diff)\n\t\t\t\t}\n\t\t\t\tfmt.Fprintf(&amp;sb, \"%s\", newChar)\n\t\t\t}\n\t\t}\n\t}\n\treturn sb.String()\n}\n\n\/\/ transformWithDistance Allows transformation based on distance.\nfunc transformWithDistance(s string, d int) string {\n\n\trotateFunction := func(r rune) rune {\n\t\tswitch {\n\t\tcase r >= 'a' &amp;&amp; r &lt;= 'z':\n\t\t\tval := (r - 'a' + rune(d))\n\t\t\tif val &lt; 0 &amp;&amp; d &lt; 0 {\n\t\t\t\treturn 'z' + ((val + 1) % 26)\n\t\t\t}\n\t\t\treturn 'a' + (val % 26)\n\n\t\tdefault:\n\t\t\tlog.Printf(\" Not a charcter: %s\", string(r))\n\t\t}\n\t\treturn ' '\n\t}\n\n\treturn strings.Map(rotateFunction, s)\n\n}<\/code><\/pre>\n\n\n\n<p>The basic functions that invoke these encode and decode functions are as under<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Encode Implements the interface Cipher\nfunc (e Encrypt) Encode(s string) (o string) {\n\to = \"\"\n\ts = removeAllOtherCharcterBeforeEncoding(s)\n\n\tif s == \"\" || len(s) &lt;= 0 {\n\t\treturn\n\t}\n\n\t\/\/ Iterate throught the runes\n\n\tswitch {\n\tcase e.distance != 0:\n\t\to = strings.ReplaceAll(transformWithDistance(s, e.distance), \" \", \"\")\n\tdefault:\n\t\to = transformWithString(s, e.key, true)\n\t}\n\n\treturn\n}\n\n\/\/ Decode decides the encrypted string\nfunc (e Encrypt) Decode(s string) (o string) {\n\to = \"\"\n\n\tlog.Printf(\" Decode: %s\", s)\n\n\tif s == \"\" || len(s) &lt;= 0 {\n\t\treturn\n\t}\n\n\tswitch {\n\tcase e.distance != 0:\n\t\tlog.Printf(\" Distance: %d\", e.distance)\n\t\to = transformWithDistance(s, -e.distance)\n\tdefault:\n\t\to = transformWithString(s, e.key, false)\n\t}\n\n\treturn\n}<\/code><\/pre>\n\n\n\n<p>Now the general feeling after writing this was I have too much the same code that I should remove for example<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>transformWithDistance<\/li><li>transformWithString<\/li><\/ul>\n\n\n\n<p>They are essentially doing the same thing?<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Version 2&#8230;. to follow soon.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hello back again, Golang is just a new syntax for me. Even though behind the scenes they developers have simplified many things, for a consumer like me writing a program is more about learning the new syntax and getting familiarized with the control and stack. In my constant quest to try something different, today I [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":647,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"footnotes":""},"categories":[34],"tags":[67,68,23],"class_list":["post-645","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-technical","tag-algo","tag-cipher","tag-golang"],"_links":{"self":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/645","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/comments?post=645"}],"version-history":[{"count":0,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/posts\/645\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media\/647"}],"wp:attachment":[{"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/media?parent=645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/categories?post=645"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.samarthya.me\/wps\/wp-json\/wp\/v2\/tags?post=645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}