2 people like it.

SHA512 hash code from a string

Generates SHA512 hash code from a string. Usually used in some kind of validity checks, e.g. check first timestamp and then check SHA-hash over id and timestamp to ensure a valid session.

1: 
2: 
3: 
4: 
5: 
6: 
7: 
8: 
9: 
let generateHash text = 
    let getbytes : (string->byte[]) = System.Text.Encoding.UTF8.GetBytes
    use algorithm = new System.Security.Cryptography.SHA512Managed()
    text |> (getbytes >> algorithm.ComputeHash >> System.Convert.ToBase64String)


//use example:
let id, time, secretkey = "1", System.DateTime.Now.ToString("yyyyMMddhhmmss"), "mysecret"
generateHash (secretkey + id + time)
val generateHash : text:string -> string

Full name: Script.generateHash
val text : string
val getbytes : (string -> byte [])
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = System.String

Full name: Microsoft.FSharp.Core.string
Multiple items
val byte : value:'T -> byte (requires member op_Explicit)

Full name: Microsoft.FSharp.Core.Operators.byte

--------------------
type byte = System.Byte

Full name: Microsoft.FSharp.Core.byte
namespace System
namespace System.Text
type Encoding =
  member BodyName : string
  member Clone : unit -> obj
  member CodePage : int
  member DecoderFallback : DecoderFallback with get, set
  member EncoderFallback : EncoderFallback with get, set
  member EncodingName : string
  member Equals : value:obj -> bool
  member GetByteCount : chars:char[] -> int + 3 overloads
  member GetBytes : chars:char[] -> byte[] + 5 overloads
  member GetCharCount : bytes:byte[] -> int + 2 overloads
  ...

Full name: System.Text.Encoding
property System.Text.Encoding.UTF8: System.Text.Encoding
System.Text.Encoding.GetBytes(s: string) : byte []
System.Text.Encoding.GetBytes(chars: char []) : byte []
System.Text.Encoding.GetBytes(chars: char [], index: int, count: int) : byte []
System.Text.Encoding.GetBytes(chars: nativeptr<char>, charCount: int, bytes: nativeptr<byte>, byteCount: int) : int
System.Text.Encoding.GetBytes(s: string, charIndex: int, charCount: int, bytes: byte [], byteIndex: int) : int
System.Text.Encoding.GetBytes(chars: char [], charIndex: int, charCount: int, bytes: byte [], byteIndex: int) : int
val algorithm : System.Security.Cryptography.SHA512Managed
namespace System.Security
namespace System.Security.Cryptography
Multiple items
type SHA512Managed =
  inherit SHA512
  new : unit -> SHA512Managed
  member Initialize : unit -> unit

Full name: System.Security.Cryptography.SHA512Managed

--------------------
System.Security.Cryptography.SHA512Managed() : unit
System.Security.Cryptography.HashAlgorithm.ComputeHash(buffer: byte []) : byte []
System.Security.Cryptography.HashAlgorithm.ComputeHash(inputStream: System.IO.Stream) : byte []
System.Security.Cryptography.HashAlgorithm.ComputeHash(buffer: byte [], offset: int, count: int) : byte []
type Convert =
  static val DBNull : obj
  static member ChangeType : value:obj * typeCode:TypeCode -> obj + 3 overloads
  static member FromBase64CharArray : inArray:char[] * offset:int * length:int -> byte[]
  static member FromBase64String : s:string -> byte[]
  static member GetTypeCode : value:obj -> TypeCode
  static member IsDBNull : value:obj -> bool
  static member ToBase64CharArray : inArray:byte[] * offsetIn:int * length:int * outArray:char[] * offsetOut:int -> int + 1 overload
  static member ToBase64String : inArray:byte[] -> string + 3 overloads
  static member ToBoolean : value:obj -> bool + 17 overloads
  static member ToByte : value:obj -> byte + 18 overloads
  ...

Full name: System.Convert
System.Convert.ToBase64String(inArray: byte []) : string
System.Convert.ToBase64String(inArray: byte [], options: System.Base64FormattingOptions) : string
System.Convert.ToBase64String(inArray: byte [], offset: int, length: int) : string
System.Convert.ToBase64String(inArray: byte [], offset: int, length: int, options: System.Base64FormattingOptions) : string
val id : string

Full name: Script.id
val time : string

Full name: Script.time
val secretkey : string

Full name: Script.secretkey
Multiple items
type DateTime =
  struct
    new : ticks:int64 -> DateTime + 10 overloads
    member Add : value:TimeSpan -> DateTime
    member AddDays : value:float -> DateTime
    member AddHours : value:float -> DateTime
    member AddMilliseconds : value:float -> DateTime
    member AddMinutes : value:float -> DateTime
    member AddMonths : months:int -> DateTime
    member AddSeconds : value:float -> DateTime
    member AddTicks : value:int64 -> DateTime
    member AddYears : value:int -> DateTime
    ...
  end

Full name: System.DateTime

--------------------
System.DateTime()
   (+0 other overloads)
System.DateTime(ticks: int64) : unit
   (+0 other overloads)
System.DateTime(ticks: int64, kind: System.DateTimeKind) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, calendar: System.Globalization.Calendar) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: System.DateTimeKind) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: System.Globalization.Calendar) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
   (+0 other overloads)
System.DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: System.DateTimeKind) : unit
   (+0 other overloads)
property System.DateTime.Now: System.DateTime
System.DateTime.ToString() : string
System.DateTime.ToString(provider: System.IFormatProvider) : string
System.DateTime.ToString(format: string) : string
System.DateTime.ToString(format: string, provider: System.IFormatProvider) : string

More information

Link:http://fssnip.net/4P
Posted:6 years ago
Author:Tuomas Hietanen
Tags: sha , hashing