<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Ramon Saccilotto on Medium]]></title>
        <description><![CDATA[Stories by Ramon Saccilotto on Medium]]></description>
        <link>https://medium.com/@tikiatua?source=rss-9cdc4fa1fd29------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*lsGs1vuO4HNnYgk5gzrjHQ.jpeg</url>
            <title>Stories by Ramon Saccilotto on Medium</title>
            <link>https://medium.com/@tikiatua?source=rss-9cdc4fa1fd29------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Wed, 18 Nov 2026 07:37:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@tikiatua/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Soft Hyphens With Swift]]></title>
            <link>https://medium.com/@tikiatua/soft-hyphens-with-swift-edaf2160eaa8?source=rss-9cdc4fa1fd29------2</link>
            <guid isPermaLink="false">https://medium.com/p/edaf2160eaa8</guid>
            <category><![CDATA[typography]]></category>
            <category><![CDATA[ios]]></category>
            <category><![CDATA[swift]]></category>
            <dc:creator><![CDATA[Ramon Saccilotto]]></dc:creator>
            <pubDate>Wed, 30 Mar 2016 12:42:34 GMT</pubDate>
            <atom:updated>2016-03-30T12:42:48.736Z</atom:updated>
            <content:encoded><![CDATA[<p>So this is gonna be a very short story that might be useful for people that are working on iOS projects with non english languages and long words that should be conditionally hyphenated at a specific place (looking at you German).</p><p>When building an iOS-App with ResearchKit, we quickly realized that the text in longer question titles is often not layouted very nicely — specifically without hyphenation.</p><p>To fix this, you can simply add so called “soft hyphens” into your strings that will trigger a conditional hyphen if needed. With Swift, the trick is to use the unicode character <a href="http://www.fileformat.info/info/unicode/char/00AD/index.htm">0x00AD</a> in your strings as described below.</p><pre># Swift 2.2<br>let title = &quot;Haben Sie etwaige Ver\u{00AD}bes\u{00AD}serungs\u{00AD}vor\u{00AD}schläge?&quot;</pre><p>As a side note: Soft hyphens can also be used on websites using the &amp;shy; html entity.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=edaf2160eaa8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Symmetric and Asymmetric Encryption with Javascript and Go]]></title>
            <link>https://medium.com/@tikiatua/symmetric-and-asymmetric-encryption-with-javascript-and-go-240043e56daf?source=rss-9cdc4fa1fd29------2</link>
            <guid isPermaLink="false">https://medium.com/p/240043e56daf</guid>
            <category><![CDATA[golang]]></category>
            <category><![CDATA[javascript]]></category>
            <category><![CDATA[encryption]]></category>
            <dc:creator><![CDATA[Ramon Saccilotto]]></dc:creator>
            <pubDate>Fri, 23 Jan 2015 09:27:27 GMT</pubDate>
            <atom:updated>2018-12-31T14:42:01.786Z</atom:updated>
            <content:encoded><![CDATA[<p>Let’s say you have a use case for encrypting data in a browser with javascript and want to decrypt it on a Go server later on (although some people might argue, that doing stuff in the browser is never really secure).</p><p>Such a use case might be a mobile PhoneGap application that is shared by multiple users and might not always have a connection to the server. You could encrypt the data using an asymmetric encryption — i.e. a public rsa key to encrypt the data, store the encrypted data temporarily on the device and only decrypt the data once it has been transmitted to the server (as a side note: please use a ssl/tls connection to transmit the data).</p><p>So what would we need to do that:</p><ul><li>A javascript library for asymmetric data encryption</li><li>A javascript library for symmetric data encryption (in case our message/data is quite long)</li><li>The crypto packages in the go standard library</li></ul><h3>Encrypt the Data on the Client</h3><p>We will start by encrypting our data on the client side with the help of two awesome javascript libraries:</p><ul><li>cryptojs/aes: available at <a href="https://code.google.com/p/crypto-js/#AES"><em>https://code.google.com/p/crypto-js/#AES</em></a></li><li>jsencrypt: available at <a href="https://github.com/travist/jsencrypt"><em>https://github.com/travist/jsencrypt</em></a></li></ul><p>Our goal is to encrypt the data on the client in such a way, that it cannot be unencrypted without having a key that is not stored on the client itself.</p><p>This can be achieved by means of using a so called asymmetric encryption using a public key to encrypt the data and a separate private key to decrypt it again. The public key would be stored on the client. After encryption, the data could only be decrypted with the private key, which is only available on the server.</p><p>However, if you have a lot of data, the process of asymmetric encryption &amp; decryption might take up quite some time and resources and you will need very long encryption keys to do so.</p><p>Therefor we can use a slightly different approach.</p><ol><li>We will generate a random encryption-key of an appropriate length</li><li>We encrypt our data with a symmetric encryption method, that uses the same key for encryption and decryption. In our case we are going to use the AES-encryption method</li><li>We encrypt our key (for symmetric encryption) with our public key</li><li>We send or store only the asymmetric encrypted key and the symmetric encrypted data.</li><li>On the server we decrypt the encryption-key with our private key and use the result to decrypt our data.</li><li>Et voila..</li></ol><p>Please note, that this data encryption does not necessarily authenticate the client. Anyone could send data to the server if the public key is publicly available. Therefore it is recommended to either secure access to the public key or use other means of authenticating a client (i.e. an api-token or json-web-tokens).</p><p>First we are going to need a javascript function to generate a random key.</p><pre>// define the characters to pick from<br><strong>var</strong> chars<strong> =<br>  </strong>&quot;0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmn<br>   opqrstuvwxyz*&amp;-%/!?*+=()&quot;;</pre><pre><em>// create a key for symmetric encryption<br>// pass in the desired length of your key<br></em><strong>var</strong> generateKey<strong> =</strong> function generateKey(keyLength){</pre><pre><strong>  var</strong> randomstring<strong> =</strong> &#39;&#39;;<br>  <br>  for (<strong>var</strong> i<strong>=</strong>0; i <strong>&lt;</strong> keyLength; i<strong>++</strong>) {<br>    <strong>var</strong> rnum<strong> =</strong> Math.floor(Math.random() <strong>*</strong> chars.length);<br>    randomstring <strong>+=</strong> chars.substring(rnum,rnum<strong>+</strong>1);<br>  }</pre><pre>  <strong>return</strong> randomstring;</pre><pre>};</pre><p>Next we need a public and private key. You can use openssl to easily generate some keys using the following commands:</p><pre><em>// rsa keys can be easiliy created with openssl<br></em><strong><em>&gt; openssl genrsa -out rsa_4096_priv.pem 4096<br>&gt; openssl rsa -pubout -in rsa_4096_priv.pem -out rsa_4096_pub.pem</em></strong></pre><p>Alternatively you can use this site by Travis Tidwell — the guy who wrote the jsencrypt-library — to generate your keys.</p><p><a href="http://travistidwell.com/blog/2013/09/06/an-online-rsa-public-and-private-key-generator">An online RSA public and private key generator - Travis Tidwell</a></p><p>Please note, that I would generally not recommend to generate the keys anywhere else than your own machine for security reasons.</p><p>Now we encrypt our data symmetrically using the cryptojs-library and encrypt the respective key asymmetrically using the jsencrypt library.</p><p><strong>It is important to note, that the cryptojs library will generate its own 256bit key from the supplied key.</strong> Therefore, to be able to decrypt the data later on, we have to extract the generated key and its initialization vector (iv) from the object that is returned by the CryptoJS.AES.encrypt-method.</p><p><strong>The key and initialization vector are automatically hex-encoded by the CryptoJs library, whereas the data is automatically base64 encoded</strong> (this is very important when trying to decrypt the data on the server).</p><p>Afterwards we can use the jsencrypt library to encrypt this key.</p><pre><em>// copy the content of the file rsa_4096_pub.pem into a variable</em><strong> <br>var</strong> publicKey = &quot;-----BEGIN PUBLIC KEY-----MIICI[...]&quot;;</pre><pre><em>// encrypt a javascript object into a payload to be sent<br>// to a server or stored on the client<br></em><strong>var</strong> encrypt = function encrypt(dataObject, publicKey) {<em><br> <br>  // Create a new encryption key (with a specified length)</em><br>  <strong>var </strong>key <strong>= </strong>generateKey(50);</pre><pre>  <em>// convert data to a json string</em><br>  <strong>var </strong>dataAsString <strong>= </strong>JSON.stringify(data);</pre><pre>  <em>// encrypt the data symmetrically <br>  // (the cryptojs library will generate its own 256bit key!!)</em><br>  <strong>var </strong>aesEncrypted <strong>= </strong>CryptoJS.AES.encrypt(dataAsString, key);</pre><pre>  // get the symmetric key and initialization vector from<br>  // (hex encoded) and concatenate them into one string<br>  <strong>var</strong> aesKey = aesEncrypted.key + &quot;:::&quot; + aesEncrypted.iv;</pre><pre>  // the data is base64 encoded <br>  <strong>var</strong> encryptedMessage = aesEncrypted.toString();</pre><pre><br>  // we create a new JSEncrypt object for rsa encryption<br>  <strong>var</strong> rsaEncrypt = new JSEncrypt();<br>  <br>  // we set the public key (which we passed into the function)<br>  rsaEncrypt.setPublicKey(publicKey);</pre><pre>  // now we encrypt the key &amp; iv with our public key<br>  <strong>var</strong> encryptedKey = rsaEncrypt.encrypt(aesKey);</pre><pre>  // and concatenate our payload message<br>  <strong>var</strong> payload = encryptedKey + &quot;:::&quot; + encryptedMessage;</pre><pre>  <strong>return</strong> payload;</pre><pre>};</pre><p>That is it for the first part. Now let’s pretend we sent the data to a server running Go and want to decrypt it there.</p><h3>Decrypt the Data on the Server with Go</h3><p>Please note that you should have some basic understanding of go to understand the upcoming part.</p><p>Fortunately we can decrypt the data using only packages from the standard library — so you will not have to depend on any third-party libraries.</p><p>Let’s create our go file with an initialization method.</p><p>In addition we are going to define a Data-struct to hold our decrypted data, a constant to hold the path to our private key file, a constant for the delimiter used to separate the key and message in the encrypted string sent from the client — in our case ::: — and a variable to store our decoded private key.</p><pre><strong>package main</strong></pre><pre><strong>import</strong> (<br>  “crypto/aes”<br>  “crypto/cipher”<br>  “crypto/rand”<br>  “crypto/rsa”<br>  “crypto/x509&quot;<br>  “encoding/base64&quot;<br>  “encoding/hex”<br>  “encoding/json”<br>  “encoding/pem”<br>  “io/ioutil”<br>  “log”<br>  “strings”<br>)</pre><pre><em>// structure to hold our decrypted data</em><br><strong>type</strong> Data struct {<br>  Message String<br>}</pre><pre><em>// set a constant to our private key file<br>// and the string we are using to separate the encrypted <br>// symmetric key from the encrypted message received <br>// from the client</em><br><strong>const</strong> (<br>  PrivateKeyPath string = “rsa_4096_priv.pem”<br>  MessageKeyDelimiter string = “:::”<br>)</pre><pre><em>// use a variable to store the decoded private key</em><br><strong>var</strong> PrivateKey *rsa.PrivateKey</pre><p>Now we create an init function — which will be run before our main function — to initialize our private key. You could of course move this code into its own function and just call it before you do any decryption.</p><pre>func <strong>init</strong>() {<br>  <br>  <em>// read the private key for data decryption</em><br>  keyFile, err := ioutil.ReadFile(<strong>PrivateKeyPath</strong>)<br>  if err != nil {<br>    log.Fatal(“Error reading private key”)<br>  }</pre><pre>  <em>// get the private key pem block data</em><br>  block, _ := pem.Decode(keyFile)<br>  if block == nil {<br>    log.Fatal(“Error reading private key”)<br>  }</pre><pre>  <em>// decode the RSA private key</em><br>  PrivateKey, err = x509.ParsePKCS1PrivateKey(block.Bytes)<br>  if err != nil {<br>    log.Fatal(“Could not decode our “)<br>  }</pre><pre>}</pre><p>Let’s go ahead and create our main function and use golangs amazing concurrency features.</p><p>Please note, that I did not include the server methods required to receive the data from the client. But you should be able to do it yourselves (otherwise please leave me a comment).</p><p>First, we create a channel that will receive the encrypted data from the client and a channel to which we will pass the decrypted data for further processing.</p><p>Second, we implement a decoding function that will be run in its own go routine.</p><pre>func <strong>main</strong>() {</pre><pre>  <em>// please note, that you need some method to<br>  // receive the data from somewhere (i.e. server, function call)</em></pre><pre> <em> // create a channel to receive encrypted data<br>  // that will then be decrypted<br>  </em><strong>decoderChannel</strong> := make(chan string)</pre><pre>  <em>// create a channel to receive decrypted data</em><br>  <strong>messageChannel</strong> := make(chan Data)</pre><pre>  <em>// run our decoding function in a separate go routine</em><br>  go <strong>decode</strong>(decoderChannel, messageChannel)</pre><pre>  <em>// do something with the decrypted messages<br>  // (using a for loop to never stop receiving data<br>  </em>for {<br>    fmt.Println(<strong>&lt;-messageChannel</strong>)<br>  }</pre><pre>  <em>// we should never get to this code as the receiving<br>  // channel in the line before is blocking forever</em><br>  log.Println(“this should never be called”)</pre><pre>}</pre><p>Our next step is to implement the decode function.</p><pre><em>// decode is used to decode encrypted data</em><br>func <strong>decode</strong>(<strong>decoderChannel</strong> &lt;-chan string, <strong>messageChannel</strong> chan&lt;- Data) {<br>   <br>  <em>// run a loop to never stop receiving data on our<br>  // decoder channel  </em><br>  for {</pre><pre>    <em>// split the received message <br>    // (encrypted key is appended to data)<br>    // data[0]:key, data[1]:message<br>    // the MessageKeyDelimiter is saved as constant</em><br>    encrypted := strings.Split(<strong>&lt;-decoderChannel</strong>, <strong>MessageKeyDelimiter</strong>)</pre><pre>    <em>// decrypt the AES-encryption-key with our private key</em><br>    <strong>keyAndIv</strong> := decryptRSA(encrypted[0])</pre><pre>    <em>// split the key and its components</em><br>    <strong>keyComponents</strong> := strings.Split(keyAndIv, MessageKeyDelimiter)</pre><pre>    <em>// decrypt the AES encrypted data using <br>    // the components from before</em><br>    <strong>message</strong> := decryptAES(keyComponents[0], keyComponents[1], encrypted[1])</pre><pre>    <em>// create a new data struct</em><br>    <strong>data</strong> := Data{}</pre><pre>    <em>// parse the json data into our struct</em><br>    json.Unmarshal(<strong>message</strong>, <strong>&amp;data</strong>)</pre><pre>   <em> // pass the data to our message channel for further work</em><br>    <strong>messageChannel</strong> &lt;- data</pre><pre>  }</pre><pre>}</pre><p>And all that is left is to do is to implement the decryptRSA and decryptAES functions.</p><pre><em>// decryptRSA will decrypt an rsa-encrypted string using the <br>// private key loaded in the init-function from the specified<br>// private-key-file. It will return the decrypted data as string. </em><br>func <strong>decryptRSA</strong>(<strong>encrypted</strong> string) <strong>string</strong> {<br>  <br>  <em>// decode our encrypted string into cipher bytes</em><br>  <strong>cipheredValue</strong>, err := <strong>base64</strong>.StdEncoding.DecodeString(encrypted)<br>  if err != nil {<br>    log.Println(“error: decoding string (rsa)”)<br>    return “”<br>  }</pre><pre>  <em>// decrypt the data</em><br>  <strong>var out []byte</strong><br>  <strong>out</strong>, err = rsa.DecryptPKCS1v15(rand.Reader, PrivateKey, cipheredValue)<br>  if err != nil {<br>    log.Println(“error: reading encrypted data”)<br>    return “”<br>  }</pre><pre>  return <strong>string(out)</strong></pre><pre>}</pre><pre>// decryptAES will decrpyt an encrypted message using the <br>// given key and iv. It will return the decrypted information<br>// as byte-slice<br>func <strong>decryptAES</strong>(<strong>keyString</strong> string, <strong>ivString</strong> string, <strong>encrypted</strong> string) <strong>[]byte</strong> {</pre><pre>  <em>// decode from hex to byte</em><br>  key, _ := hex.DecodeString(keyString)<br>  iv, _ := hex.DecodeString(ivString)</pre><pre>  <em>// decode our encrypted string into bytes</em><br>  cipheredMessage, err := base64.StdEncoding.DecodeString(encrypted)<br>  if err != nil {<br>    log.Println(“error: decoding string (aes)”)<br>  }</pre><pre>  <em>// create a new cipher block from our key</em><br>  block, err := aes.NewCipher(key)<br>  if err != nil {<br>    log.Println(err)<br>    return nil<br>  }</pre><pre>  <em>// cbc message must be multiple of aes blocksize</em><br>  if len(cipheredMessage) &lt; aes.BlockSize {<br>    log.Println(“error: ciphertext too short”)<br>  }</pre><pre>  <em>// the iv is prepended to the actual message/data</em><br>  cipherText := cipheredMessage[aes.BlockSize:]</pre><pre>  <em>// decrypt the data</em><br>  mode := cipher.NewCBCDecrypter(block, iv)<br>  mode.CryptBlocks(cipherText, cipherText)</pre><pre>  return cipherText</pre><pre>}</pre><p>I hope this article was of some use to you. Please let me know if you have any questions or suggestions for improvement.</p><p>Stay tuned for a full demo-application on github.. [written March 6th 15]</p><p>References:</p><ul><li>AES encryption with Go <a href="https://www.socketloop.com/tutorials/golang-how-to-encrypt-with-aes-crypto">https://www.socketloop.com/tutorials/golang-how-to-encrypt-with-aes-crypto</a></li><li>AES encryption with CryptoJS javascript library <a href="https://code.google.com/p/crypto-js/#AES">https://code.google.com/p/crypto-js/#AES</a></li><li>RSA encryption with JSEncrypt javascript library <a href="http://travistidwell.com/jsencrypt/">http://travistidwell.com/jsencrypt/</a></li><li>How to choose an AES encryption algorithm <a href="http://stackoverflow.com/questions/1220751/how-to-choose-an-aes-encryption-mode-cbc-ecb-ctr-ocb-cfb">http://stackoverflow.com/questions/1220751/how-to-choose-an-aes-encryption-mode-cbc-ecb-ctr-ocb-cfb</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=240043e56daf" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>