Click here to Skip to main content
15,903,203 members
Home / Discussions / Algorithms
   

Algorithms

 
AnswerRe: how to implement this Algorithm? (for DOM) Pin
Richard MacCutchan21-Sep-13 7:13
mveRichard MacCutchan21-Sep-13 7:13 
QuestionI need help on Genetic Algorithm using C# Pin
Uche Osahor4-Sep-13 2:37
Uche Osahor4-Sep-13 2:37 
AnswerRe: I need help on Genetic Algorithm using C# Pin
Alan Balkany4-Sep-13 5:00
Alan Balkany4-Sep-13 5:00 
GeneralRe: I need help on Genetic Algorithm using C# Pin
Uche Osahor4-Sep-13 5:08
Uche Osahor4-Sep-13 5:08 
Questioninversion algorithm Pin
amnakhan78612-Aug-13 18:51
amnakhan78612-Aug-13 18:51 
GeneralRe: inversion algorithm Pin
harold aptroot15-Aug-13 21:43
harold aptroot15-Aug-13 21:43 
AnswerRe: inversion algorithm Pin
Alan Balkany16-Aug-13 4:31
Alan Balkany16-Aug-13 4:31 
QuestionTLS HMAC and the Pseudorandom Function Simplification Assistance Pin
Dominick Marciano10-Aug-13 19:37
professionalDominick Marciano10-Aug-13 19: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_<hash>(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.
Question[Closure and System.Reflection] Pin
Member 823782310-Aug-13 1:43
Member 823782310-Aug-13 1:43 
QuestionGARCH algo Pin
ExcellentOrg5-Aug-13 22:46
ExcellentOrg5-Aug-13 22:46 
QuestionHow to find the shortest path between two nodes in a undigraph and it should via several given nodes? Pin
ahuzhangbo2-Aug-13 19:08
ahuzhangbo2-Aug-13 19:08 
AnswerRe: How to find the shortest path between two nodes in a undigraph and it should via several given nodes? Pin
Argonia14-Aug-13 5:10
professionalArgonia14-Aug-13 5:10 
GeneralRe: How to find the shortest path between two nodes in a undigraph and it should via several given nodes? Pin
ahuzhangbo14-Aug-13 15:30
ahuzhangbo14-Aug-13 15:30 
GeneralRe: How to find the shortest path between two nodes in a undigraph and it should via several given nodes? Pin
Argonia14-Aug-13 21:36
professionalArgonia14-Aug-13 21:36 
GeneralRe: How to find the shortest path between two nodes in a undigraph and it should via several given nodes? Pin
ahuzhangbo15-Aug-13 0:21
ahuzhangbo15-Aug-13 0:21 
GeneralRe: How to find the shortest path between two nodes in a undigraph and it should via several given nodes? Pin
Argonia15-Aug-13 1:22
professionalArgonia15-Aug-13 1:22 
GeneralRe: How to find the shortest path between two nodes in a undigraph and it should via several given nodes? Pin
ahuzhangbo15-Aug-13 16:03
ahuzhangbo15-Aug-13 16:03 
AnswerRe: How to find the shortest path between two nodes in a undigraph and it should via several given nodes? Pin
Member 1222963626-Dec-15 6:59
Member 1222963626-Dec-15 6:59 
Questionneed help in algorithum Pin
snehal1221-Aug-13 21:16
snehal1221-Aug-13 21:16 
GeneralRe: need help in algorithum Pin
harold aptroot1-Aug-13 22:05
harold aptroot1-Aug-13 22:05 
GeneralRe: need help in algorithum Pin
Richard MacCutchan1-Aug-13 23:16
mveRichard MacCutchan1-Aug-13 23:16 
GeneralRe: need help in algorithum Pin
Bernhard Hiller2-Aug-13 0:02
Bernhard Hiller2-Aug-13 0:02 
AnswerRe: need help in algorithum Pin
SoMad2-Aug-13 0:08
professionalSoMad2-Aug-13 0:08 
GeneralRe: need help in algorithum Pin
koll Zhu22-Sep-13 23:55
koll Zhu22-Sep-13 23:55 
QuestionCubic Spline Interpolation [Answered] Pin
Skippums18-Jul-13 15:02
Skippums18-Jul-13 15:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.