Friday, November 28, 2014

In this post, I continue to talk about the Ruby on Rails MVC. We will look at a few examples:
Here's the view to list all shares:
<h1>Listing shares</h1>
<%= link_to 'New share', new_share_path %>

<table>
  <tr>
    <th>Id</th>
    <th>Path</th>
    <th>Rowguid</th>
    <th>Action</th>
  </tr>

  <% @shares.each do |share| %>
    <tr>
      <td><%= share['id'] %></td>
      <td><%= share['path'] %></td>
      <td><%= share['rowguid'] %></td>
      <td><%= link_to 'Destroy', share_path(share),
                    method: :delete, data: { confirm: 'Are you sure?' }
      %></td>
    </tr>
  <% end %>
</table>
and here is a way to add new share:
<%= form_for :shareurl: shares_path do |f%>

  <p>
    <%= f.label :sharepath %><br>
    <%= f.text_area :sharepath %>
  </p>

  <p>
    <%= f.submit %>
  </p>
<% end %>
The form submits the data to the controller as parameters.

The data forwarded from the controller to the view is usually a model instance or an object. If the object is a dictionary the elements are accessed by their keys. For model instances, they are accessed by members.
#codingexercise
Int GetDistinctMode(int [] A)
{
if (A == null) return 0;
return A.DistinctMode();
}

class OneTimePasswordAlgorithm:
       doubleDigits = [  0, 2, 4, 6, 8, 1, 3, 5, 7, 9 ]
       def calcChecksum(self, num, digits):
           doubleDigit = True
           total = 0
           for i in range(len(digits)-1) :
               digits -= 1
               digit = num % 10
               num /= 10
               if doubleDigit:
                   digit = self.doubleDigits[digit]
               total += digit
               doubleDigit =  not doubleDigit
           result = total % 10
           if result > 0:
               result = 10 - result
           return result

      DIGITS_POWER = [1,10,100,1000,10000,100000,1000000,10000000,100000000 ] 
      def generateTOTP(self, key, time, returnDigits, crypto):  
         codeDigits = int(returnDigits 
         result = '' 
         #Using the counter  
         #First 8 bytes are for the movingFactor  
         #Compliant with base RFC 4226 (HOTP)  
         while (len(time) < 16 ):  
             time = "0" + time  
         #Get the HEX in a Byte[]  
         msg = bytearray(time.decode("hex"))  
         k = bytearray(key.decode("hex"))   
         import hmac  
         hash = bytearray(hmac.new(k,msg,crypto).hexdigest()) 
         #put selected bytes into result int  
         offset = int(int(hash[len(hash) - 1]) & 0xf)  
         binary =  ((hash[offset] & 0x7f) << 24) |  ((hash[offset + 1] & 0xff) << 16) |  ((hash[offset + 2] & 0xff) << 8) | (hash[offset + 3] & 0xff)  
         otp = int(binary % self.DIGITS_POWER[codeDigits])  
         result = str(otp);  
         while (len(result) < codeDigits):   
             result = "0" + result 
         return result 
>>> otp = OneTimePasswordAlgorithm() 
>>> import hashlib 
>>> otp.generateTOTP('90AB891C', '123465539', '6', hashlib.sha256) 
'857652' 
>>>

No comments:

Post a Comment