SHA2 Hash Function
Developer Guide
SHA2 Hash Function
FraudLabs Pro uses SHA2 to hash the sensitive values, such as, email_hash, username_hash, password_hash and card_hash. User is required to hash these values prior to API request using the sample codes given below, otherwise the API will returns you the error.
Online Hash Generator
To confirm you have implemented the hashing correctly, you can compare the result with our hash generator.
Sample Codes
PHP
<?php
function
fraudlabspro_hash
(
$s
)
{
$hash
=
'fraudlabspro_'
.
$s
;
for
(
$i
=
0
;
$i
<
65536
;
$i
++
)
{
$hash
=
sha1
(
'fraudlabspro_'
.
$hash
)
;
}
$hash2
=
hash
(
'sha256'
,
$hash
)
;
return
$hash2
;
}
?>
Java
import
java
.
security
.
MessageDigest
;
import
java
.
util
.
Formatter
;
public
class
main
{
public
static
String
SHA1
(
String
s
)
throws
Exception
{
MessageDigest
sha1
=
MessageDigest
.
getInstance
(
"SHA1"
)
;
Formatter
formatter
=
new
Formatter
(
)
;
sha1
.
update
(
s
.
getBytes
(
)
)
;
byte
[
]
hash
=
sha1
.
digest
(
)
;
for
(
byte
b
:
hash
)
{
formatter
.
format
(
"%02x"
,
b
)
;
}
return
formatter
.
toString
(
)
;
}
public
static
String
fraudlabspro_hash
(
String
s
)
throws
Exception
{
Integer
i
=
0
;
String
hash
=
"fraudlabspro_"
+
s
;
for
(
i
=
0
;
i
<
65536
;
i
++
)
{
hash
=
SHA1
(
"fraudlabspro_"
+
hash
)
;
}
MessageDigest
sha256
=
MessageDigest
.
getInstance
(
"SHA-256"
)
;
byte
[
]
hashBytes
=
sha256
.
digest
(
hash
.
getBytes
(
)
)
;
StringBuilder
hash2
=
new
StringBuilder
(
)
;
for
(
byte
b
:
hashBytes
)
{
hash2
.
append
(
String
.
format
(
"%02x"
,
b
)
)
;
}
return
hash2
.
toString
(
)
;
}
}
VB .NET
Imports
System
Imports
System
.
Security
.
Cryptography
Imports
System
.
Text
Public
Function
fraudlabspro_hash
(
ByVal
s
As
String
)
As
String
Dim
i
As
Integer
=
0
Dim
hash
As
String
=
"fraudlabspro_"
+
s
For
i
=
1
To
65536
hash
=
ComputeSHA1Hash
(
"fraudlabspro_"
+
hash
)
Next
Using
mysha
=
SHA256
.
Create
(
)
Dim
inputBytes
As
Byte
(
)
=
Encoding
.
UTF8
.
GetBytes
(
hash
)
Dim
hashBytes
As
Byte
(
)
=
mysha
.
ComputeHash
(
inputBytes
)
Dim
hash2
As
New
StringBuilder
(
)
For
Each
b
As
Byte
In
hashBytes hash2
.
AppendFormat
(
"{0:x2}"
,
b
)
Next
Return
hash2
.
ToString
(
)
End
Using
End
Function
Public
Function
ComputeSHA1Hash
(
ByVal
s
As
String
)
As
String
Dim
bytes
(
)
As
Byte
Dim
x
As
String
=
""
Using
mysha
=
SHA1
.
Create
(
)
bytes
=
Encoding
.
ASCII
.
GetBytes
(
s
)
bytes
=
mysha
.
ComputeHash
(
bytes
)
For
Each
b
As
Byte
In
bytes x
+
=
b
.
ToString
(
"x2"
)
Next
Return
x
End
Using
End
Function
C#
using
System
.
Security
.
Cryptography
;
using
System
.
Text
;
public
string
fraudlabspro_hash
(
string
s
)
{
int
i
=
0
;
string
hash
=
"fraudlabspro_"
+
s
;
for
(
i
=
1
;
i
<=
65536
;
i
++
)
{
hash
=
ComputeSHA1Hash
(
"fraudlabspro_"
+
hash
)
;
}
using
(
SHA256
mysha
=
SHA256
.
Create
(
)
)
{
byte
[
]
inputBytes
=
Encoding
.
UTF8
.
GetBytes
(
hash
)
;
byte
[
]
hashBytes
=
mysha
.
ComputeHash
(
inputBytes
)
;
StringBuilder
hash2
=
new
StringBuilder
(
)
;
foreach
(
byte
b
in
hashBytes
)
{
hash2
.
AppendFormat
(
"{0:x2}"
,
b
)
;
}
return
hash2
.
ToString
(
)
;
}
}
public
string
ComputeSHA1Hash
(
string
s
)
{
string
x
=
""
;
using
(
SHA1
mysha
=
SHA1
.
Create
(
)
)
{
byte
[
]
bytes
=
Encoding
.
ASCII
.
GetBytes
(
s
)
;
bytes
=
mysha
.
ComputeHash
(
bytes
)
;
foreach
(
byte
b
in
bytes
)
{
x
+=
b
.
ToString
(
"x2"
)
;
}
return
x
;
}
}
Python
import
hashlib
def
fraudlabspro_hash
(
s
)
:
hash
=
"fraudlabspro_"
+
s
for
i
in
range
(
65536
)
:
hash
=
hashlib
.
sha1
(
(
"fraudlabspro_"
+
hash
)
.
encode
(
'utf-8'
)
)
.
hexdigest
(
)
hash2
=
hashlib
.
sha256
(
hash
.
encode
(
)
)
.
hexdigest
(
)
return
hash2