FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Nethereum/src/Nethereum.KeyStore/KeyStoreService.cs at master · Nethereum/Nethereum · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
Nethereum
/
Nethereum
Public
Notifications
You must be signed in to change notification settings
Fork
743
Star
2.3k
Code
Issues
103
Pull requests
5
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Nethereum
/
src
/
Nethereum.KeyStore
/
KeyStoreService.cs
Copy path
More file actions
More file actions
Latest commit
History
History
History
98 lines (84 loc) · 3.84 KB
Breadcrumbs
Nethereum
/
src
/
Nethereum.KeyStore
/
KeyStoreService.cs
Copy path
File metadata and controls
98 lines (84 loc) · 3.84 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
using
System
;
using
System
.
IO
;
using
System
.
Text
;
using
Newtonsoft
.
Json
.
Linq
;
namespace
Nethereum
.
KeyStore
{
public
class
KeyStoreService
{
private
readonly
KeyStoreKdfChecker
_keyStoreKdfChecker
;
private
readonly
KeyStoreScryptService
_keyStoreScryptService
;
private
readonly
KeyStorePbkdf2Service
_keyStorePbkdf2Service
;
public
KeyStoreService
(
)
{
_keyStoreKdfChecker
=
new
KeyStoreKdfChecker
(
)
;
_keyStorePbkdf2Service
=
new
KeyStorePbkdf2Service
(
)
;
_keyStoreScryptService
=
new
KeyStoreScryptService
(
)
;
}
public
KeyStoreService
(
KeyStoreKdfChecker
keyStoreKdfChecker
,
KeyStoreScryptService
keyStoreScryptService
,
KeyStorePbkdf2Service
keyStorePbkdf2Service
)
{
_keyStoreKdfChecker
=
keyStoreKdfChecker
;
_keyStoreScryptService
=
keyStoreScryptService
;
_keyStorePbkdf2Service
=
keyStorePbkdf2Service
;
}
public
string
GetAddressFromKeyStore
(
string
json
)
{
if
(
json
==
null
)
throw
new
ArgumentNullException
(
nameof
(
json
)
)
;
var
keyStoreDocument
=
JObject
.
Parse
(
json
)
;
return
keyStoreDocument
[
"address"
]
.
Value
<
string
>
(
)
;
}
public
string
GenerateUTCFileName
(
string
address
)
{
if
(
address
==
null
)
throw
new
ArgumentNullException
(
nameof
(
address
)
)
;
return
"UTC--"
+
DateTime
.
UtcNow
.
ToString
(
"O"
)
.
Replace
(
":"
,
"-"
)
+
"--"
+
address
.
Replace
(
"0x"
,
""
)
;
}
#if
!
PCL
public
byte
[
]
DecryptKeyStoreFromFile
(
string
password
,
string
filePath
)
{
using
(
var
file
=
File
.
OpenText
(
filePath
)
)
{
var
json
=
file
.
ReadToEnd
(
)
;
return
DecryptKeyStoreFromJson
(
password
,
json
)
;
}
}
#endif
public
byte
[
]
DecryptKeyStoreFromJson
(
string
password
,
string
json
)
{
if
(
password
==
null
)
throw
new
ArgumentNullException
(
nameof
(
password
)
)
;
if
(
json
==
null
)
throw
new
ArgumentNullException
(
nameof
(
json
)
)
;
var
type
=
_keyStoreKdfChecker
.
GetKeyStoreKdfType
(
json
)
;
if
(
type
==
KeyStoreKdfChecker
.
KdfType
.
pbkdf2
)
return
_keyStorePbkdf2Service
.
DecryptKeyStoreFromJson
(
password
,
json
)
;
if
(
type
==
KeyStoreKdfChecker
.
KdfType
.
scrypt
)
return
_keyStoreScryptService
.
DecryptKeyStoreFromJson
(
password
,
json
)
;
//shold not reach here, already handled by the checker
throw
new
Exception
(
"Invalid kdf type"
)
;
}
public
string
EncryptAndGenerateDefaultKeyStoreAsJson
(
string
password
,
byte
[
]
key
,
string
address
)
{
if
(
password
==
null
)
throw
new
ArgumentNullException
(
nameof
(
password
)
)
;
if
(
address
==
null
)
throw
new
ArgumentNullException
(
nameof
(
address
)
)
;
return
_keyStoreScryptService
.
EncryptAndGenerateKeyStoreAsJson
(
password
,
key
,
address
)
;
}
public
string
EncryptPayloadAsJson
(
string
password
,
byte
[
]
data
)
{
if
(
data
==
null
)
throw
new
ArgumentNullException
(
nameof
(
data
)
)
;
return
_keyStoreScryptService
.
EncryptAndGenerateCryptoStoreAsJson
(
password
,
data
)
;
}
public
string
EncryptPayloadFromStringAsJson
(
string
password
,
string
data
)
{
if
(
data
==
null
)
throw
new
ArgumentNullException
(
nameof
(
data
)
)
;
return
_keyStoreScryptService
.
EncryptAndGenerateCryptoStoreFromStringAsJson
(
password
,
data
)
;
}
public
byte
[
]
DecryptPayloadFromJson
(
string
password
,
string
json
)
{
return
_keyStoreScryptService
.
DecryptCryptoStoreFromJson
(
password
,
json
)
;
}
public
string
DecryptPayloadToUtf8String
(
string
password
,
string
json
)
{
var
bytes
=
DecryptPayloadFromJson
(
password
,
json
)
;
return
Encoding
.
UTF8
.
GetString
(
bytes
)
;
}
}
}
Back
|
FazBrowse Home
|
New Git URL