├── .github └── workflows │ └── update-compiled-libraries.yml ├── .gitignore ├── .gitmodules ├── KeySharp.sln ├── KeySharp ├── ErrorType.cs ├── Glue.cs ├── KeySharp.csproj ├── Keyring.cs ├── KeyringException.cs └── runtimes │ ├── linux-x64 │ └── native │ │ └── libskeychain.so │ ├── osx-arm64 │ └── native │ │ └── libskeychain.dylib │ ├── osx-x64 │ └── native │ │ └── libskeychain.dylib │ └── win-x64 │ └── native │ └── skeychain.dll ├── LICENSE.txt ├── README.md ├── Tester └── Tester.csproj ├── native ├── CMakeLists.txt ├── src │ ├── main.cpp │ └── main.h └── toolchain │ └── mingw64.cmake └── testing └── testing.csproj /.github/workflows/update-compiled-libraries.yml: -------------------------------------------------------------------------------- 1 | name: Update compiled libraries 2 | on: 3 | push: 4 | branches: 5 | - master 6 | paths: 7 | - .github/workflows/update-compiled-libraries.yml 8 | - native/**/* 9 | workflow_dispatch: 10 | jobs: 11 | build: 12 | strategy: 13 | matrix: 14 | include: 15 | - os: macos-latest 16 | runtime: osx-arm64 17 | lib: libskeychain.dylib 18 | - os: ubuntu-latest 19 | runtime: linux-x64 20 | lib: libskeychain.so 21 | - os: windows-latest 22 | runtime: win-x64 23 | lib: skeychain.dll 24 | max-parallel: 1 # to avoid conflicts during git push 25 | fail-fast: true 26 | runs-on: ${{ matrix.os }} 27 | steps: 28 | - uses: actions/checkout@v4.1.0 29 | with: 30 | ref: master 31 | submodules: true 32 | - run: cmake -DCMAKE_BUILD_TYPE=Release -S native -B native 33 | - run: cmake --build native 34 | # On windows, MSBuild puts dll under "debug" folder 35 | - run: cp native/${{ matrix.os == 'windows-latest' && 'debug/skeychain.dll' || matrix.lib }} KeySharp/runtimes/${{ matrix.runtime }}/native/${{ matrix.lib }} 36 | - if: matrix.os == 'macos-latest' 37 | run: cp native/${{ matrix.lib }} KeySharp/runtimes/osx-x64/native/${{ matrix.lib }} 38 | - run: git add KeySharp/runtimes 39 | - run: git config --global user.name "github-actions[bot]" 40 | - run: git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com" 41 | - run: git commit -m "[${{ matrix.runtime == 'osx-arm64' && 'osx-arm64/osx-x64' || matrix.runtime }}] Update ${{ matrix.lib }}" 42 | - run: git push 43 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # ASP.NET Scaffolding 66 | ScaffoldingReadMe.txt 67 | 68 | # StyleCop 69 | StyleCopReport.xml 70 | 71 | # Files built by Visual Studio 72 | *_i.c 73 | *_p.c 74 | *_h.h 75 | *.ilk 76 | *.meta 77 | *.obj 78 | *.iobj 79 | *.pch 80 | *.pdb 81 | *.ipdb 82 | *.pgc 83 | *.pgd 84 | *.rsp 85 | *.sbr 86 | *.tlb 87 | *.tli 88 | *.tlh 89 | *.tmp 90 | *.tmp_proj 91 | *_wpftmp.csproj 92 | *.log 93 | *.tlog 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio 6 auto-generated project file (contains which files were open etc.) 298 | *.vbp 299 | 300 | # Visual Studio 6 workspace and project file (working project files containing files to include in project) 301 | *.dsw 302 | *.dsp 303 | 304 | # Visual Studio 6 technical files 305 | *.ncb 306 | *.aps 307 | 308 | # Visual Studio LightSwitch build output 309 | **/*.HTMLClient/GeneratedArtifacts 310 | **/*.DesktopClient/GeneratedArtifacts 311 | **/*.DesktopClient/ModelManifest.xml 312 | **/*.Server/GeneratedArtifacts 313 | **/*.Server/ModelManifest.xml 314 | _Pvt_Extensions 315 | 316 | # Paket dependency manager 317 | .paket/paket.exe 318 | paket-files/ 319 | 320 | # FAKE - F# Make 321 | .fake/ 322 | 323 | # CodeRush personal settings 324 | .cr/personal 325 | 326 | # Python Tools for Visual Studio (PTVS) 327 | __pycache__/ 328 | *.pyc 329 | 330 | # Cake - Uncomment if you are using it 331 | # tools/** 332 | # !tools/packages.config 333 | 334 | # Tabs Studio 335 | *.tss 336 | 337 | # Telerik's JustMock configuration file 338 | *.jmconfig 339 | 340 | # BizTalk build output 341 | *.btp.cs 342 | *.btm.cs 343 | *.odx.cs 344 | *.xsd.cs 345 | 346 | # OpenCover UI analysis results 347 | OpenCover/ 348 | 349 | # Azure Stream Analytics local run output 350 | ASALocalRun/ 351 | 352 | # MSBuild Binary and Structured Log 353 | *.binlog 354 | 355 | # NVidia Nsight GPU debugger configuration file 356 | *.nvuser 357 | 358 | # MFractors (Xamarin productivity tool) working folder 359 | .mfractor/ 360 | 361 | # Local History for Visual Studio 362 | .localhistory/ 363 | 364 | # Visual Studio History (VSHistory) files 365 | .vshistory/ 366 | 367 | # BeatPulse healthcheck temp database 368 | healthchecksdb 369 | 370 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 371 | MigrationBackup/ 372 | 373 | # Ionide (cross platform F# VS Code tools) working folder 374 | .ionide/ 375 | 376 | # Fody - auto-generated XML schema 377 | FodyWeavers.xsd 378 | 379 | # VS Code files for those working on multiple tools 380 | .vscode/* 381 | !.vscode/settings.json 382 | !.vscode/tasks.json 383 | !.vscode/launch.json 384 | !.vscode/extensions.json 385 | *.code-workspace 386 | 387 | # Local History for Visual Studio Code 388 | .history/ 389 | 390 | # Windows Installer files from build outputs 391 | *.cab 392 | *.msi 393 | *.msix 394 | *.msm 395 | *.msp 396 | 397 | # JetBrains Rider 398 | *.sln.iml 399 | 400 | build 401 | .idea -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "native/lib/keychain"] 2 | path = native/lib/keychain 3 | url = https://github.com/hrantzsch/keychain 4 | -------------------------------------------------------------------------------- /KeySharp.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "KeySharp", "KeySharp\KeySharp.csproj", "{FC317BB2-271A-4917-8A8D-3E1591F831B2}" 4 | EndProject 5 | Global 6 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 7 | Debug|Any CPU = Debug|Any CPU 8 | Release|Any CPU = Release|Any CPU 9 | EndGlobalSection 10 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 11 | {FC317BB2-271A-4917-8A8D-3E1591F831B2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 12 | {FC317BB2-271A-4917-8A8D-3E1591F831B2}.Debug|Any CPU.Build.0 = Debug|Any CPU 13 | {FC317BB2-271A-4917-8A8D-3E1591F831B2}.Release|Any CPU.ActiveCfg = Release|Any CPU 14 | {FC317BB2-271A-4917-8A8D-3E1591F831B2}.Release|Any CPU.Build.0 = Release|Any CPU 15 | EndGlobalSection 16 | EndGlobal 17 | -------------------------------------------------------------------------------- /KeySharp/ErrorType.cs: -------------------------------------------------------------------------------- 1 | namespace KeySharp; 2 | 3 | /// 4 | /// Kind of error returned by the native library. 5 | /// 6 | public enum ErrorType 7 | { 8 | /// 9 | /// No error occurred. 10 | /// 11 | NoError = 0, 12 | 13 | /// 14 | /// A generic error occurred. Please check the message property. 15 | /// 16 | GenericError, 17 | 18 | /// 19 | /// Password was not found. 20 | /// 21 | NotFound, 22 | 23 | /// 24 | /// Password was too long. 25 | /// Can only happen on Windows. 26 | /// 27 | PasswordTooLong = 10, 28 | 29 | /// 30 | /// Access was denied. 31 | /// Can only happen on Mac OS X. 32 | /// 33 | AccessDenied, 34 | } -------------------------------------------------------------------------------- /KeySharp/Glue.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Runtime.InteropServices; 3 | 4 | namespace KeySharp; 5 | 6 | internal static unsafe class Glue 7 | { 8 | [DllImport("skeychain", EntryPoint = "setPassword")] 9 | public static extern bool SetPassword(byte* package, byte* service, byte* user, byte* password); 10 | 11 | [DllImport("skeychain", EntryPoint = "getPassword")] 12 | public static extern byte* GetPassword(byte* package, byte* service, byte* user); 13 | 14 | [DllImport("skeychain", EntryPoint = "deletePassword")] 15 | public static extern bool DeletePassword(byte* package, byte* service, byte* user); 16 | 17 | [DllImport("skeychain", EntryPoint = "getLastErrorMessage")] 18 | public static extern byte* GetLastErrorMessage(); 19 | 20 | [DllImport("skeychain", EntryPoint = "getLastError")] 21 | public static extern ErrorType GetLastError(); 22 | } -------------------------------------------------------------------------------- /KeySharp/KeySharp.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.1 5 | 6 | KeySharp 7 | 1.0.5 8 | 1.0.5 9 | 10 | true 11 | 12 | Cross-platform keychain 13 | 14 | goaaats 15 | latest 16 | 17 | https://github.com/goaaats/KeySharp 18 | LICENSE.txt 19 | README.md 20 | https://github.com/goaaats/KeySharp 21 | true 22 | true 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | true 32 | true 33 | snupkg 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | -------------------------------------------------------------------------------- /KeySharp/Keyring.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Runtime.InteropServices; 4 | using System.Text; 5 | 6 | namespace KeySharp; 7 | 8 | /// 9 | /// Class used to interface with the OS keyring. 10 | /// 11 | public static unsafe class Keyring 12 | { 13 | /// 14 | /// Save a password to the keyring. 15 | /// 16 | /// The package ID. 17 | /// The service name. 18 | /// The user name. 19 | /// The password to save. 20 | public static void SetPassword(string package, string service, string username, string password) 21 | { 22 | var nativePackage = AllocateNullTerminated(package); 23 | var nativeService = AllocateNullTerminated(service); 24 | var nativeUsername = AllocateNullTerminated(username); 25 | var nativePassword = AllocateNullTerminated(password); 26 | 27 | var ret = Glue.SetPassword(nativePackage, nativeService, nativeUsername, nativePassword); 28 | 29 | if (!ret) 30 | { 31 | ThrowLastError(); 32 | } 33 | 34 | Util.Free(nativePackage); 35 | Util.Free(nativeService); 36 | Util.Free(nativeUsername); 37 | Util.Free(nativePassword); 38 | } 39 | 40 | /// 41 | /// Get a password from the keyring. 42 | /// 43 | /// The package ID. 44 | /// The service name. 45 | /// The user name. 46 | /// The saved password. 47 | public static string GetPassword(string package, string service, string username) 48 | { 49 | var nativePackage = AllocateNullTerminated(package); 50 | var nativeService = AllocateNullTerminated(service); 51 | var nativeUsername = AllocateNullTerminated(username); 52 | 53 | var ret = Glue.GetPassword(nativePackage, nativeService, nativeUsername); 54 | 55 | if (ret == null) 56 | { 57 | ThrowLastError(); 58 | } 59 | 60 | Util.Free(nativePackage); 61 | Util.Free(nativeService); 62 | Util.Free(nativeUsername); 63 | 64 | return ReadString(ret); 65 | } 66 | 67 | /// 68 | /// Delete a password. 69 | /// 70 | /// The package ID. 71 | /// The service name. 72 | /// The user name. 73 | public static void DeletePassword(string package, string service, string username) 74 | { 75 | var nativePackage = AllocateNullTerminated(package); 76 | var nativeService = AllocateNullTerminated(service); 77 | var nativeUsername = AllocateNullTerminated(username); 78 | 79 | var ret = Glue.DeletePassword(nativePackage, nativeService, nativeUsername); 80 | 81 | if (!ret) 82 | { 83 | ThrowLastError(); 84 | } 85 | 86 | Util.Free(nativePackage); 87 | Util.Free(nativeService); 88 | Util.Free(nativeUsername); 89 | } 90 | 91 | private static string ReadString(byte* data) 92 | { 93 | var passData = new List(); 94 | var c = *data; 95 | while (c != 0) 96 | { 97 | passData.Add(c); 98 | c = *(data + 1); 99 | data++; 100 | } 101 | 102 | return Encoding.UTF8.GetString(passData.ToArray()); 103 | } 104 | 105 | private static void ThrowLastError() 106 | { 107 | var errorMsg = Glue.GetLastErrorMessage(); 108 | var msgString = "Unknown error"; 109 | 110 | if (errorMsg != null) 111 | msgString = ReadString(errorMsg); 112 | 113 | throw new KeyringException(Glue.GetLastError(), msgString); 114 | } 115 | 116 | private static byte* AllocateNullTerminated(string text) 117 | { 118 | byte* native; 119 | if (text != null) 120 | { 121 | var byteCount = Encoding.UTF8.GetByteCount(text); 122 | native = Util.Allocate(byteCount + 1); 123 | var nativeLabelOffset = Util.GetUtf8(text, native, byteCount); 124 | native[nativeLabelOffset] = 0; 125 | } 126 | else { native = null; } 127 | 128 | return native; 129 | } 130 | } 131 | 132 | internal static unsafe class Util 133 | { 134 | internal static byte* Allocate(int byteCount) => (byte*)Marshal.AllocHGlobal(byteCount); 135 | 136 | internal static void Free(byte* ptr) => Marshal.FreeHGlobal((IntPtr)ptr); 137 | 138 | internal static int GetUtf8(string s, byte* utf8Bytes, int utf8ByteCount) 139 | { 140 | fixed (char* utf16Ptr = s) 141 | { 142 | return Encoding.UTF8.GetBytes(utf16Ptr, s.Length, utf8Bytes, utf8ByteCount); 143 | } 144 | } 145 | } -------------------------------------------------------------------------------- /KeySharp/KeyringException.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace KeySharp; 4 | 5 | /// 6 | /// Exception thrown when an error during keyring operations occurs. 7 | /// 8 | public class KeyringException : Exception 9 | { 10 | /// 11 | /// The type of error that occurred. 12 | /// 13 | public ErrorType Type { get; private set; } 14 | 15 | /// 16 | /// The message returned by the keyring backend. 17 | /// 18 | public string BackendMessage { get; private set; } 19 | 20 | internal KeyringException(ErrorType type, string message) : base($"{message} ({type})") 21 | { 22 | Type = type; 23 | BackendMessage = message; 24 | } 25 | } -------------------------------------------------------------------------------- /KeySharp/runtimes/linux-x64/native/libskeychain.so: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vedantmgoyal9/KeySharp/af4c907db1a30bc355eff1a60cfb54b07090fbce/KeySharp/runtimes/linux-x64/native/libskeychain.so -------------------------------------------------------------------------------- /KeySharp/runtimes/osx-arm64/native/libskeychain.dylib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vedantmgoyal9/KeySharp/af4c907db1a30bc355eff1a60cfb54b07090fbce/KeySharp/runtimes/osx-arm64/native/libskeychain.dylib -------------------------------------------------------------------------------- /KeySharp/runtimes/osx-x64/native/libskeychain.dylib: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vedantmgoyal9/KeySharp/af4c907db1a30bc355eff1a60cfb54b07090fbce/KeySharp/runtimes/osx-x64/native/libskeychain.dylib -------------------------------------------------------------------------------- /KeySharp/runtimes/win-x64/native/skeychain.dll: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/vedantmgoyal9/KeySharp/af4c907db1a30bc355eff1a60cfb54b07090fbce/KeySharp/runtimes/win-x64/native/skeychain.dll -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright (c) 2021 goaaats 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # KeySharp [![Nuget](https://img.shields.io/nuget/v/KeySharp)](https://www.nuget.org/packages/KeySharp/) 2 | 3 | Cross-platform OS keyring access for C#/.NET based on [keychain by hrantzsch](https://github.com/hrantzsch/keychain). 4 | All calls are potentially blocking, as the OS may ask the user to grant access or unlock the keychain. 5 | 6 | ### Example 7 | 8 | ```csharp 9 | Keyring.SetPassword("com.example.test", "TestService", "user", "password"); 10 | 11 | try { 12 | var password = Keyring.GetPassword("com.example.test", "TestService", "user"); 13 | } catch (KeyringException ex) // Thrown if password was not saved 14 | // handle 15 | } 16 | 17 | Keyring.DeletePassword("com.example.test", "TestService", "user"); 18 | ``` 19 | 20 | ### Native libraries 21 | The precompiled shared libraries in this repository are based on the code in the `native` folder, wrapping the keychain library by hrantzsch. 22 | | Platform | Compiler | 23 | |----------|----------| 24 | | win-x64 | Visual C++ 2022 (Keychain library doesn't support mingw-w64 at the moment, TBD) | 25 | | linux-x64 | GCC 11.2.0(x86_64-pc-linux-gnu) | 26 | | osx-x64 | AppleClang 13.0.0.13000029 (universal), macOS 10.11+ | 27 | | osx-arm64 | AppleClang 13.0.0.13000029 (universal), macOS 10.11+, take care: you need to sign your binaries to run on m1! | -------------------------------------------------------------------------------- /Tester/Tester.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net6.0 5 | enable 6 | enable 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /native/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.12.4) 2 | 3 | cmake_policy(SET CMP0054 NEW) 4 | 5 | set (CMAKE_OSX_ARCHITECTURES "x86_64;arm64" CACHE STRING "macOS Build architectures" FORCE) 6 | 7 | set (PROJECT_VERSION "1.0") 8 | 9 | # Set the variable PROJ_NAME to whatever your library's name is, PROJECT_VERSION should be a version string like "0.1" 10 | project(skeychain VERSION ${PROJECT_VERSION}) 11 | 12 | # To build shared libraries in Windows, we set CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS to TRUE. 13 | # See https://cmake.org/cmake/help/v3.4/variable/CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS.html 14 | # See https://blog.kitware.com/create-dlls-on-windows-without-declspec-using-new-cmake-export-all-feature/ 15 | #set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON) 16 | 17 | # Create our library target 18 | add_library(skeychain SHARED) 19 | 20 | target_sources(skeychain 21 | PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp 22 | ) 23 | 24 | # This will name your output .so files "libsomething.1.0" which is pretty useful 25 | #set_target_properties(skeychain 26 | #PROPERTIES 27 | # VERSION ${PROJECT_VERSION} 28 | # SOVERSION ${PROJECT_VERSION} 29 | #) 30 | 31 | # Let's set compiler-specific flags 32 | if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU") 33 | # G++ 34 | target_compile_options(skeychain PRIVATE -Wall -Wextra) 35 | message("IS G++!!") 36 | elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") 37 | # MSVC 38 | target_compile_options(skeychain PRIVATE /EHsc /W2 /c) 39 | # Set the DLLEXPORT variable to export symbols 40 | target_compile_definitions(skeychain PRIVATE WIN_EXPORT) 41 | message("IS MSVC!!") 42 | elseif(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") 43 | # Activate all clang warnings except those that don't really affect much 44 | target_compile_options(roukavici PRIVATE -Weverything -Wno-c++98-compat -Wno-c++98-compat-pedantic -Wno-newline-eof -Wno-padded -Wno-exit-time-destructors -Wno-global-constructors -Wno-constant-conversion) 45 | endif() 46 | 47 | message(${CMAKE_CXX_COMPILER_ID}) 48 | 49 | # We can now add their CMakeList as a child to ours. 50 | add_subdirectory(./lib/keychain/) 51 | 52 | # And now, we tell CMake that libdependency should be linked to our library 53 | target_link_libraries(skeychain PRIVATE keychain) 54 | -------------------------------------------------------------------------------- /native/src/main.cpp: -------------------------------------------------------------------------------- 1 | #include "main.h" 2 | 3 | #include 4 | 5 | keychain::Error* lastError = nullptr; 6 | 7 | extern "C" { 8 | 9 | EXPORTED bool setPassword(const char* package, const char* service, const char* user, const char* password) 10 | { 11 | keychain::Error* error = new keychain::Error(); 12 | keychain::setPassword(package, service, user, password, *error); 13 | 14 | if (error->type != keychain::ErrorType::NoError) 15 | { 16 | lastError = error; 17 | return false; 18 | } 19 | 20 | delete error; 21 | 22 | return true; 23 | } 24 | 25 | EXPORTED const char* getPassword(const char* package, const char* service, const char* user) 26 | { 27 | keychain::Error* error = new keychain::Error(); 28 | auto password = keychain::getPassword(package, service, user, *error); 29 | 30 | if (error->type != keychain::ErrorType::NoError) 31 | { 32 | lastError = error; 33 | return nullptr; 34 | } 35 | 36 | char* writable = new char[password.size() + 1]; 37 | std::copy(password.begin(), password.end(), writable); 38 | writable[password.size()] = '\0'; 39 | 40 | delete error; 41 | 42 | return writable; 43 | } 44 | 45 | EXPORTED bool deletePassword(const char* package, const char* service, const char* user) 46 | { 47 | keychain::Error* error = new keychain::Error(); 48 | keychain::deletePassword(package, service, user, *error); 49 | 50 | if (error->type != keychain::ErrorType::NoError) 51 | { 52 | lastError = error; 53 | return false; 54 | } 55 | 56 | delete error; 57 | 58 | return true; 59 | } 60 | 61 | EXPORTED const char* getLastErrorMessage() 62 | { 63 | if (!lastError) 64 | return nullptr; 65 | 66 | char* writable = new char[lastError->message.size() + 1]; 67 | std::copy(lastError->message.begin(), lastError->message.end(), writable); 68 | writable[lastError->message.size()] = '\0'; 69 | 70 | return writable; 71 | } 72 | 73 | EXPORTED keychain::ErrorType getLastError() 74 | { 75 | if (!lastError) 76 | return keychain::ErrorType::NoError; 77 | 78 | return lastError->type; 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /native/src/main.h: -------------------------------------------------------------------------------- 1 | #pragma once 2 | 3 | // Define EXPORTED for any platform 4 | #if defined _WIN32 || defined __CYGWIN__ 5 | #ifdef WIN_EXPORT 6 | // Exporting... 7 | #ifdef __GNUC__ 8 | #define EXPORTED __attribute__ ((dllexport)) 9 | #else 10 | #define EXPORTED __declspec(dllexport) // Note: actually gcc seems to also supports this syntax. 11 | #endif 12 | #else 13 | #ifdef __GNUC__ 14 | #define EXPORTED __attribute__ ((dllimport)) 15 | #else 16 | #define EXPORTED __declspec(dllimport) // Note: actually gcc seems to also supports this syntax. 17 | #endif 18 | #endif 19 | #define NOT_EXPORTED 20 | #else 21 | #if __GNUC__ >= 4 22 | #define EXPORTED __attribute__ ((visibility ("default"))) 23 | #define NOT_EXPORTED __attribute__ ((visibility ("hidden"))) 24 | #else 25 | #define EXPORTED 26 | #define NOT_EXPORTED 27 | #endif 28 | #endif -------------------------------------------------------------------------------- /native/toolchain/mingw64.cmake: -------------------------------------------------------------------------------- 1 | # the name of the target operating system 2 | SET(CMAKE_SYSTEM_NAME Windows) 3 | 4 | # which compilers to use for C and C++ 5 | SET(CMAKE_C_COMPILER x86_64-w64-mingw32-gcc) 6 | SET(CMAKE_CXX_COMPILER x86_64-w64-mingw32-g++) 7 | # here is the target environment located 8 | SET(CMAKE_FIND_ROOT_PATH /usr/x86_64-w64-mingw32) 9 | # adjust the default behaviour of the FIND_XXX() commands: 10 | # search headers and libraries in the target environment, search 11 | # programs in the host environment 12 | set(CMAKE_FIND_ROOT_PATH_MODE_PROGRAM NEVER) 13 | set(CMAKE_FIND_ROOT_PATH_MODE_LIBRARY ONLY) 14 | set(CMAKE_FIND_ROOT_PATH_MODE_INCLUDE ONLY) -------------------------------------------------------------------------------- /testing/testing.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | net6.0 6 | enable 7 | enable 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | --------------------------------------------------------------------------------