15,798,592 members
Home / Discussions / Algorithms

# Algorithms

 Re: how to implement this Algorithm? (for DOM) Richard MacCutchan21-Sep-13 8:13 Richard MacCutchan 21-Sep-13 8:13
 I need help on Genetic Algorithm using C# Uche Osahor4-Sep-13 3:37 Uche Osahor 4-Sep-13 3:37
 Re: I need help on Genetic Algorithm using C# Alan Balkany4-Sep-13 6:00 Alan Balkany 4-Sep-13 6:00
 Re: I need help on Genetic Algorithm using C# Uche Osahor4-Sep-13 6:08 Uche Osahor 4-Sep-13 6:08
 inversion algorithm amnakhan78612-Aug-13 19:51 amnakhan786 12-Aug-13 19:51
 Re: inversion algorithm harold aptroot15-Aug-13 22:43 harold aptroot 15-Aug-13 22:43
 Re: inversion algorithm Alan Balkany16-Aug-13 5:31 Alan Balkany 16-Aug-13 5:31
 TLS HMAC and the Pseudorandom Function Simplification Assistance Dominick Marciano10-Aug-13 20:37 Dominick Marciano 10-Aug-13 20:37
 I'm currently studying cryptography implementation in depth. To that end I'm reading various RFCs and trying to implement the algorithms so I can get a better understanding of them and hopefully make my applications more secure when cryptography is used. Right now I'm reading the TLS RFC 5246. The first algorithm in the document is a pseudorandom function (PRF) that takes a secret, a seed, and a label and produces an output of a specified length. This is Section 5 (Page 14 in the PDF) of the document. It defines a function called `P_hash(secret, data)` that uses a single hash function to expand a secret and seed to an arbitrary length: Pseudo-code (Page 15): ```P_hash(secret, seed) = HMAC_hash(secret, A(1) + seed) + HMAC_hash(secret, A(2) + seed) + HMAC_hash(secret, A(3) + seed) + ...``` where + indicates concatenation. `A()` is defined as: ```A(0) = seed A(i) = HMAC_hash(secret, A(i-1))``` `P_hash` is iterated as many times as necessary to produce the required length. The example given in the RFC is if `P_SHA256` is being used to create 80 bytes, it will be iterated three times (through `A(3)`), creating 96 bytes of data where the last 16 bytes of the final iteration is discarded to leave the needed 80 bytes. The PRF is created by applying `P_hash` to the secret as follows: `PRF(secret, label, seed) = P_(secret, label + seed)` I've defined the two functions (`PRF` & `P_hash`) but added two additional parameters; the first if `reqLength` to set the output length and `HmacLength` which is an enum that will restrict the allowed hash function to SHA256, SHA384, or SHA512. The code that follows I believe follows the RFC correctly, however I feel it is inefficient because of how I'm going back and forth between `List(Of Byte)` and `Byte()`'s, but I can't figure out how to simplify it; possibly because I've been programming all day or maybe because it is 1:30 AM. Either way I was hoping that someone could help simplify the code because everything I tried (like eliminating some of the loops) resulted in the code not compiling for various reasons. The code I have so far is this: VB ```Public Class PRF Public Enum P_SHA HMAC_256 HMAC_384 HMAC_512 End Enum Public Function PRF(secret As Byte(), label As Byte(), seed As Byte(), reqLength As Integer, Optional HmacLength As P_SHA = P_SHA.HMAC_512) Dim temporaryArray As New List(Of Byte) temporaryArray.AddRange(label) temporaryArray.AddRange(seed) Return P_hash(secret, temporaryArray.ToArray, reqLength, HmacLength) End Function Private Function P_hash(secret As Byte(), seed As Byte(), reqLength As Integer, Optional HmacLength As P_SHA = P_SHA.HMAC_512) As Byte() Dim data As New List(Of Byte) Dim HMAC_hash As HMAC Select Case HmacLength Case P_SHA.HMAC_256 HMAC_hash = New HMACSHA256(secret) Case P_SHA.HMAC_384 HMAC_hash = New HMACSHA384(secret) Case Else HMAC_hash = New HMACSHA512(secret) End Select Dim i As Integer = 1 Dim A As New List(Of Byte()) A.Add(seed) Dim concatenateByte As New List(Of Byte()) '<----- Dim temporaryArray As New List(Of Byte) '<----- Do Until data.Count >= reqLength concatenateByte.Clear() concatenateByte.Add(A(i - 1)) concatenateByte.Add(seed) For Each byt As Byte() In concatenateByte '<----- For Each b As Byte In byt '<----- temporaryArray.Add(b) '<----- Next '<----- Next data.AddRange(HMAC_hash.ComputeHash(temporaryArray.ToArray)) Loop If data.Count = reqLength Then Return data.ToArray Else Return data.GetRange(0, reqLength).ToArray End If End Function End Class``` The arrows indicate the parts of the code that I imagine there is a way to eliminate although I can't see it. Any suggestions or advice would be greatly appreciated. Thanks in advance.
 [Closure and System.Reflection] Member 823782310-Aug-13 2:43 Member 8237823 10-Aug-13 2:43
 GARCH algo ExcellentOrg5-Aug-13 23:46 ExcellentOrg 5-Aug-13 23:46
 How to find the shortest path between two nodes in a undigraph and it should via several given nodes? ahuzhangbo2-Aug-13 20:08 ahuzhangbo 2-Aug-13 20:08
 Re: How to find the shortest path between two nodes in a undigraph and it should via several given nodes? Argonia14-Aug-13 6:10 Argonia 14-Aug-13 6:10
 Re: How to find the shortest path between two nodes in a undigraph and it should via several given nodes? ahuzhangbo14-Aug-13 16:30 ahuzhangbo 14-Aug-13 16:30
 Re: How to find the shortest path between two nodes in a undigraph and it should via several given nodes? Argonia14-Aug-13 22:36 Argonia 14-Aug-13 22:36
 Re: How to find the shortest path between two nodes in a undigraph and it should via several given nodes? ahuzhangbo15-Aug-13 1:21 ahuzhangbo 15-Aug-13 1:21
 Re: How to find the shortest path between two nodes in a undigraph and it should via several given nodes? Argonia15-Aug-13 2:22 Argonia 15-Aug-13 2:22
 Re: How to find the shortest path between two nodes in a undigraph and it should via several given nodes? ahuzhangbo15-Aug-13 17:03 ahuzhangbo 15-Aug-13 17:03
 Re: How to find the shortest path between two nodes in a undigraph and it should via several given nodes? Member 1222963626-Dec-15 7:59 Member 12229636 26-Dec-15 7:59
 need help in algorithum snehal1221-Aug-13 22:16 snehal122 1-Aug-13 22:16
 Re: need help in algorithum harold aptroot1-Aug-13 23:05 harold aptroot 1-Aug-13 23:05
 Re: need help in algorithum Richard MacCutchan2-Aug-13 0:16 Richard MacCutchan 2-Aug-13 0:16
 Re: need help in algorithum Bernhard Hiller2-Aug-13 1:02 Bernhard Hiller 2-Aug-13 1:02