<?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 Amogh Matt on Medium]]></title>
        <description><![CDATA[Stories by Amogh Matt on Medium]]></description>
        <link>https://medium.com/@amogh.matt?source=rss-fe05dc2dd456------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*QQLgddXHgBabcqx6dJiI5w@2x.jpeg</url>
            <title>Stories by Amogh Matt on Medium</title>
            <link>https://medium.com/@amogh.matt?source=rss-fe05dc2dd456------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 21 Nov 2026 07:48:39 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@amogh.matt/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[Chirp + TOTP (Generation and Transmission)]]></title>
            <link>https://medium.com/chirp-io/chirp-totp-generation-and-transmission-6044410526a7?source=rss-fe05dc2dd456------2</link>
            <guid isPermaLink="false">https://medium.com/p/6044410526a7</guid>
            <category><![CDATA[how-to]]></category>
            <category><![CDATA[chirp]]></category>
            <category><![CDATA[data-over-sound]]></category>
            <category><![CDATA[encryption]]></category>
            <category><![CDATA[security]]></category>
            <dc:creator><![CDATA[Amogh Matt]]></dc:creator>
            <pubDate>Thu, 26 Jul 2018 16:52:24 GMT</pubDate>
            <atom:updated>2018-11-05T13:45:57.323Z</atom:updated>
            <content:encoded><![CDATA[<p>One time password(OTP) is one of the common forms of Multi-Factor Authentication present on most web applications today. In this tutorial, we will give an overview of how TOTP works and then guide you through implementing an iOS authenticator application to generate the TOTP and share it to a nearby web application using Chirp.</p><p>Here’s the full project in action</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fwww.youtube.com%2Fembed%2FVA9rXX0Xa3M%3Ffeature%3Doembed&amp;url=http%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DVA9rXX0Xa3M&amp;image=https%3A%2F%2Fi.ytimg.com%2Fvi%2FVA9rXX0Xa3M%2Fhqdefault.jpg&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=youtube" width="854" height="480" frameborder="0" scrolling="no"><a href="https://medium.com/media/1e12e4e72e668d18a24963a4916436ff/href">https://medium.com/media/1e12e4e72e668d18a24963a4916436ff/href</a></iframe><h3><strong>Background</strong></h3><p>Using just a “shared secret” as a form of authentication is vulnerable to replay attacks. A replay attack is a version of the “Man in the middle” attacks. It is an attack on the network where the hashed shared secret sent across the network is intercepted and reused to gain access.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*NH0NpU5TY80Oqc2FVSkoRA.png" /><figcaption>Replay Attack</figcaption></figure><p>TOTP (Time-based OTP) is an <a href="https://www.ietf.org/rfc/rfc6238.txt">open standard</a> algorithm that computes a one-time password using a combination of a secret key, shared between a client and the server, and the time. This prevents the user from memorizing the shared secret and makes it difficult for a replay attack, as the pin changes with time.</p><p>TOTP is an extension of <a href="https://www.ietf.org/rfc/rfc4226.txt">HOTP (HMAC based one time passwords)</a> (explained in detail below). For both HOTP and TOTP, a shared base-32 secret key is generated between the client and the server. This key is encoded with a message (in our case is the timestamp) to form the <a href="https://www.ietf.org/rfc/rfc2104.txt">HMAC-SHA1 cryptographic hash</a> payload. TOTP uses the UNIX epoch as the time, formatted in seconds. By default, we refresh the TOTP every 30 seconds. This starts at the top of the minute(xx:xx:00) and 30 seconds after the minute (xx:xx:30).</p><p>Let us try to understand how the TOTP is obtained from the HMAC-SHA-1 generated pin. An example to generate the pin using OpenSSL on a MacOS terminal is shown below.</p><pre>$ KEY=`openssl rand -hex 16`<br>$ echo $KEY<br>9925d82b3b7f87326e1552f74592bed8<br>$ date +%s | openssl sha1 -hmac “$KEY”<br>b3c377e581fff1119ae11384481ad95712da1510<br>$ date +%s | openssl sha1 -hmac “$KEY” # After 30 Seconds<br>528c777129d1ed11ed1bcd92e03495fec2332daf</pre><p>We generate a random hex string as the key, the unix epoch time is obtained using date + %s. Here we obtain the HMAC-SHA-1 string as “b3c377e581fff1119ae11384481ad95712da1510”. We notice that we obtain a 40-character hexadecimal string.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/925/1*QcZIAaj3hsmzzdrRdML-Sg.png" /><figcaption>Step 1</figcaption></figure><p>Dynamic Truncation is performed next to convert this long string into something smaller and more secure. In dynamic truncation we look at the last character of this string, that is the number ‘0’, highlighted below.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/931/1*H7XF1L-sgAYanA1oMupNdQ.png" /><figcaption>Step 2</figcaption></figure><p>To determine the truncation, we use the last character as the starting index of our string. In our example, the hexadecimal character ‘0’ when converted to decimal is also ‘0’. So starting with the 0th Index we read the next 31-bits to obtain our dynamically truncated string.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1001/1*okSJV70aLmGdEToDEJ_LRg.png" /><figcaption>Step 3</figcaption></figure><p>From the original HMAC-SHA-1 hash of “b3c377e581fff1119ae11384481ad95712da1510”, we have dynamically truncated it to “B3C377E5”.</p><p>The final step to obtain the pin is to convert this truncated string into a ‘decimal’. We can do it on the terminal by running the commands below.</p><pre>$ echo “ibase=16; B3C377E5” | bc<br>3015931877</pre><p>Finally, we pick the required number of digits needed for the pin from this decimal value. If we need 6 digits, we modulo 1,000,000 and we get the last 6 digits and we end up with <strong>TOTP = 931877</strong></p><h3><strong>What are we trying to accomplish?</strong></h3><p>The procedure for generation of TOTP pin is automated. The verification the of TOTP pin is automated.<br>But we still manually input the pin digits every single time. There are instances where we type in a couple of digits and the PIN changes (Oh! drat).</p><blockquote><strong>Our mission at Chirp is to make everyday tasks like this simpler. Tasks that are frustrating for humans but easy for machines</strong>.</blockquote><h3>Building the Chirp + TOTP iOS Application</h3><p>The Chirp Connect SDK enables your apps to send and receive data using sound. The Chirp Connect SDK encodes the generated TOTP as an audio signal and then transmits it to the web application.</p><p>Before we begin, let’s make sure we have the following set up.</p><p><strong>Xcode</strong><br>Install from the App Store</p><p><strong>Chirp iOS SDK</strong><br>Sign up for it <a href="https://developers.chirp.io/sign-up">here</a></p><p>We will develop the application using <strong>Objective-C</strong>, start by setting up the project.</p><ol><li>Create an Xcode project.</li><li>Download the latest Chirp iOS SDK from <a href="https://developers.chirp.io/downloads">here</a></li><li>Follow steps at the <a href="https://developers.chirp.io/docs/getting-started/ios">Chirp Developers Page</a> to integrate Chirp into Xcode. This involves importing the framework and updating your info.plist file to include microphone usage.</li><li>With the above completed, we are all set to start coding the application. Start by initialising the Chirp Connect in the `viewDidLoad` method. <a href="https://developers.chirp.io/">Chirp Developer Hub</a> should also provide your appropriate key and secret.</li></ol><pre>self.connect = [[ChirpConnect alloc] initWithAppKey:CHIRP_APP_KEY andSecret:CHIRP_APP_SECRET];</pre><pre>[self.connect getLicenceStringWithCompletion:^(NSString * _Nullable licence, NSError * _Nullable error) <br>{<br>   // set the licence<br>   NSError *licenceError = [self.connect setLicenceString:licence];</pre><pre>   // start the SDK<br>   NSError *startError = [self.connect start];<br>}];</pre><p>5. To begin the integration, we would need the following classes, <br>a. Base32Additions — Needed to generate an HMAC-SHA-1 value of the secret key. <br>b. OTPGenerator — Needed to generate the PIN.<br>c. TOTPGenerator—An extension of the OTPGenerator class which utilises the Timestamp to generate the PIN.<br>Drag and drop the Classes folder from the Chirp iOS examples <a href="https://github.com/chirp/chirp-ios-examples">repository</a> into to your project.</p><blockquote>This consists of the required Base32 additions for `NSString` and `NSData` as well as the barebones google-authenticator classes from the opensource Google repo.<br>NOTE: Classes obtained from b and c are not ARC-compliant so you would have to set the Objective-C Automatic Reference Counting flag to NO in the Project Build Settings.</blockquote><p>Before, we write the function to utilise the aforementioned classes and generate our PIN.<br>We need to calculate the timestamp using details from the inbuilt `NSDate` class, we can write the following function to obtain that</p><pre>- (NSTimeInterval)calculateTimeInterval<br>{<br>    NSDate *now = [NSDate date];<br>    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];<br>    [dateFormatter setDateFormat:@”yyyy-MM-dd HH:mm:ss”];<br>    NSTimeZone *timeZone = [NSTimeZone timeZoneWithAbbreviation:@”GMT”];<br>    [dateFormatter setTimeZone:timeZone];</pre><pre>    long timestamp = (long)[now timeIntervalSince1970];<br>    if(timestamp % [self.expiryTextField.text integerValue] != 0)<br>    {<br>       timestamp -= timestamp % [self.expiryTextField.text integerValue];<br>    }</pre><pre>    return ([[NSString stringWithFormat:@”%ld”,timestamp] integerValue]);<br>}</pre><p>Perform the Base32 Encoding on our shared secret key and pass it onto the TOTP Generator along with the calculated timestamp</p><pre>- (void)generatePIN<br>{<br>   NSString *secret = YOUR_SHARED_SECRET;<br>   NSData *secretData = [NSData dataWithBase32String:[secret base32String]];</pre><pre>   NSTimeInterval timestamp = [self calculateTimeInterval];</pre><pre>   TOTPGenerator *generator = [[TOTPGenerator alloc] initWithSecret:secretData algorithm:kOTPGeneratorSHA1Algorithm digits:YOUR_NUM_DIGITS period:YOUR_REFRESH_PERIOD];<br> <br>   NSString *pin = [generator generateOTPForDate:[NSDate dateWithTimeIntervalSince1970:timestamp]];<br>}</pre><p>Chirp payloads are byte arrays represented by the `NSData` type. Since our generated PIN is an `NSString` object, we need to create a method to encode the message.</p><pre>-(NSData *) encodeMessage:(NSString *)message<br>{<br>   NSString *string = [NSString stringWithUTF8String:message.UTF8String];<br>   if ([string lengthOfBytesUsingEncoding:NSUTF8StringEncoding] &gt; [self.connect maxPayloadLength]) <br>   {<br>      return nil;<br>   }<br>   else <br>   {<br>      NSData *stringData = [string dataUsingEncoding:NSUTF8StringEncoding];<br>      return [self.connect isValidPayload:stringData] ? stringData : nil;<br>   }<br>}</pre><p>Now we are ready to send the PIN, lets create a button to send the PIN as a sequence of tones using Chirp.</p><pre>- (IBAction)sendButtonPressed:(id)sender<br>{<br>   NSString *chirpPIN = self.PINLabel.text;<br>   NSData *data = [self encodeMessage:chirpPIN];<br>   [self.connect send:data];<br>}</pre><p>And voila! You should now have a simple application through which you can generate a TOTP PIN based on your secret key and transmit it over to the web application with the click of a button.</p><p>You can find the complete Chirp + TOTP integrated Demo iOS App <a href="https://github.com/chirp/chirp-ios-examples">here</a></p><p>Coming up next is a tutorial to integrate your web application with the receiver and authenticating your login. Watch this space!</p><p>Amogh Matt<br><a href="/service/mailto:amogh@chirp.io">amogh@chirp.io</a></p><p>Amogh is applying his MSc research in Sound and Music Computing to Chirp’s digital signal processing tech stack, measuring performance metrics and prototyping new techniques to improve performance.</p><p>Chirp is a technology company enabling a seamless transfer of digital information via soundwaves, using a device’s loudspeaker and microphone only. The transmission uses audible or inaudible ultrasound tones and takes place with no network connection. To learn more visit chirp.io</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6044410526a7" width="1" height="1" alt=""><hr><p><a href="https://medium.com/chirp-io/chirp-totp-generation-and-transmission-6044410526a7">Chirp + TOTP (Generation and Transmission)</a> was originally published in <a href="https://medium.com/chirp-io">Chirp</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>