Compare commits

...

20 Commits

Author SHA1 Message Date
Philipp Crocoll
62c361feb0 revert unintentional change 2025-07-08 17:03:32 +02:00
Philipp Crocoll
0ee2495528 disable password-based QuickUnlock when device is not protected by screen lock 2025-07-08 16:54:45 +02:00
Philipp Crocoll
7dc635a625 Update KeePass2 code for password quality estimation; add and use list of most popular passwords to account for NIST recommendation of using "blocklists" 2025-07-08 12:09:59 +02:00
Philipp Crocoll
e15112c3b4 disable cleartextTrafficPermitted. default to https for links. 2025-07-08 10:35:50 +02:00
Philipp Crocoll
89a09ea142 set version to 1.12-r9d 2025-07-05 14:17:07 +02:00
Philipp Crocoll
628c0d2c19 enable creation of a release 2025-07-05 14:16:17 +02:00
Philipp Crocoll
584feabe44 build process: add previously missing change; fix error in build.yml, more verbose output in release.yml 2025-07-05 13:34:01 +02:00
Philipp Crocoll
ae2cfde897 change makefile to no longer use msbuild but always dotnet 2025-07-05 13:10:21 +02:00
Philipp Crocoll
42c66670b8 release process: make find calls to run in bash 2025-07-05 12:12:49 +02:00
Philipp Crocoll
288539b902 make output more verbose 2025-07-05 11:09:55 +02:00
Philipp Crocoll
61fd32f121 avoid building the "full" apk when calling apk_split 2025-07-05 09:39:54 +02:00
Philipp Crocoll
43108ec4a6 remove ls step in release 2025-07-05 08:35:58 +02:00
Philipp Crocoll
51089c6b98 change build artifact release paths. make archive name depend on matrix variables. 2025-07-05 08:16:30 +02:00
Philipp Crocoll
cf18fcf91c remove distclean from build 2025-07-05 08:15:58 +02:00
Philipp Crocoll
da0513c768 don't create release but only list files to test the release workflow without polluting github releases 2025-07-05 07:49:39 +02:00
Philipp Crocoll
37f520cdbe manifest for r9c 2025-07-05 07:40:21 +02:00
Philipp Crocoll
c98572bee0 fix wildcard for selecting files to upload to release 2025-07-05 07:39:44 +02:00
Philipp Crocoll
b1774ffc4b increase version number ot r9b 2025-07-05 07:19:26 +02:00
Philipp Crocoll
57aaa0c4cd start release action by pushing version tags instead of when release is created on Github 2025-07-05 07:18:21 +02:00
Philipp Crocoll
b961ae1b33 make sure AndroidManifest.xml exists before running nuget 2025-07-05 07:12:17 +02:00
24 changed files with 12842 additions and 2454 deletions

View File

@@ -78,7 +78,7 @@ jobs:
# - name: Build keepass2android (net) # - name: Build keepass2android (net)
# run: | # run: |
# make msbuild Flavor=Net # make dotnetbuild Flavor=Net
# - name: Build APK (net) # - name: Build APK (net)
# run: | # run: |
@@ -96,7 +96,7 @@ jobs:
# - name: Build keepass2android (nonet) # - name: Build keepass2android (nonet)
# run: | # run: |
# make msbuild Flavor=NoNet # make dotnetbuild Flavor=NoNet
# - name: Build APK (nonet) # - name: Build APK (nonet)
# run: | # run: |
@@ -212,7 +212,7 @@ jobs:
# - name: Build keepass2android (net) # - name: Build keepass2android (net)
# run: | # run: |
# make msbuild Flavor=Net # make dotnetbuild Flavor=Net
# - name: Build APK (net) # - name: Build APK (net)
# run: | # run: |
@@ -230,7 +230,7 @@ jobs:
# - name: Build keepass2android (nonet) # - name: Build keepass2android (nonet)
# run: | # run: |
# make msbuild Flavor=NoNet # make dotnetbuild Flavor=NoNet
# - name: Build APK (nonet) # - name: Build APK (nonet)
# run: | # run: |
@@ -279,7 +279,7 @@ jobs:
with: with:
minimum-size: 8GB minimum-size: 8GB
- name: Add msbuild to PATH - name: Add dotnetbuild to PATH
uses: microsoft/setup-msbuild@v2 uses: microsoft/setup-msbuild@v2
# If we want to also have nmake, use this instead # If we want to also have nmake, use this instead
#uses: ilammy/msvc-dev-cmd@v1 #uses: ilammy/msvc-dev-cmd@v1
@@ -313,12 +313,16 @@ jobs:
run: | run: |
dotnet workload update dotnet workload update
- name: Select the manifest
run: |
make manifestlink Flavor=Net
- name: Install NuGet dependencies (net) - name: Install NuGet dependencies (net)
run: make nuget Flavor=Net run: make nuget Flavor=Net
- name: Build keepass2android (net) - name: Build keepass2android (net)
run: | run: |
make msbuild Flavor=Net make dotnetbuild Flavor=Net
- name: Build APK (net) - name: Build APK (net)
run: | run: |
@@ -331,12 +335,16 @@ jobs:
path: | path: |
src/keepass2android/bin/*/*-Signed.apk src/keepass2android/bin/*/*-Signed.apk
- name: Select the manifest
run: |
make manifestlink Flavor=NoNet
- name: Install NuGet dependencies (nonet) - name: Install NuGet dependencies (nonet)
run: make nuget Flavor=NoNet run: make nuget Flavor=NoNet
- name: Build keepass2android (nonet) - name: Build keepass2android (nonet)
run: | run: |
make msbuild Flavor=NoNet make dotnetbuild Flavor=NoNet
- name: Test Autofill - name: Test Autofill
working-directory: ./src/Kp2aAutofillParser.Tests working-directory: ./src/Kp2aAutofillParser.Tests
@@ -353,5 +361,3 @@ jobs:
path: | path: |
src/keepass2android/bin/*/*-Signed.apk src/keepass2android/bin/*/*-Signed.apk
- name: Perform "make distclean"
run: make distclean

View File

@@ -3,10 +3,9 @@ env:
NAME: 'Release' NAME: 'Release'
on: on:
release: push:
types: [created] tags:
workflow_dispatch: # For manual testing - "v1.*"
jobs: jobs:
build-release: build-release:
@@ -55,7 +54,7 @@ jobs:
with: with:
minimum-size: 8GB minimum-size: 8GB
- name: Add msbuild to PATH - name: Add msbuild/dotnet to PATH
uses: microsoft/setup-msbuild@v2 uses: microsoft/setup-msbuild@v2
# If we want to also have nmake, use this instead # If we want to also have nmake, use this instead
#uses: ilammy/msvc-dev-cmd@v1 #uses: ilammy/msvc-dev-cmd@v1
@@ -79,17 +78,35 @@ jobs:
run: | run: |
make java make java
- name: List apks
run: find . -type f -name "*.apk"
shell: bash
- name: Update dotnet workloads - name: Update dotnet workloads
run: | run: |
dotnet workload update dotnet workload update
- name: Install NuGet dependencies - name: List apks
run: make nuget Flavor=${{ matrix.flavor }} run: find . -type f -name "*.apk"
shell: bash
- name: Select the manifest - name: Select the manifest
run: | run: |
make manifestlink Flavor=${{ matrix.flavor }} make manifestlink Flavor=${{ matrix.flavor }}
- name: List apks
run: find . -type f -name "*.apk"
shell: bash
- name: Install NuGet dependencies
run: make nuget Flavor=${{ matrix.flavor }}
- name: List apks
run: find . -type f -name "*.apk"
shell: bash
- name: Build APK (net) - name: Build APK (net)
env: env:
KeyStore: "${{ github.workspace }}/kp2a.keystore" KeyStore: "${{ github.workspace }}/kp2a.keystore"
@@ -103,17 +120,27 @@ jobs:
run: | run: |
make ${{ matrix.target }} Configuration=Release Flavor=${{ matrix.flavor }} make ${{ matrix.target }} Configuration=Release Flavor=${{ matrix.flavor }}
- name: List apks
run: find . -type f -name "*.apk"
shell: bash
- name: Archive production artifacts - name: Archive production artifacts
uses: actions/upload-artifact@v4 uses: actions/upload-artifact@v4
with: with:
name: signed APK (built on ${{ github.job }}) name: keepass2android_${{ matrix.target }}_${{ matrix.flavor }}
# the first line is for "apk" target, the second line is for "apk_split" target
path: | path: |
src/keepass2android-app/bin/Release/net8.0-android/*/publish/*.apk src/keepass2android-app/bin/Release/**/publish/*.apk src/keepass2android-app/bin/Release/net8.0-android/publish/*.apk
src/keepass2android-app/bin/Release/net8.0-android/*/publish/*.apk
- name: List apks
run: find . -type f -name "*.apk"
shell: bash
- name: Upload APK to GitHub Release - name: Upload APK to GitHub Release
uses: softprops/action-gh-release@v2 uses: softprops/action-gh-release@v2
if: github.ref_type == 'tag' if: github.ref_type == 'tag'
with: with:
files: | files: |
src/keepass2android-app/bin/Release/net8.0-android/*/publish/*.apk src/keepass2android-app/bin/Release/**/publish/*.apk src/keepass2android-app/bin/Release/net8.0-android/publish/*.apk
src/keepass2android-app/bin/Release/net8.0-android/*/publish/*.apk

View File

@@ -4,10 +4,10 @@
# This Makefile can be used on both unix-like (use make) & windows (with GNU make) # This Makefile can be used on both unix-like (use make) & windows (with GNU make)
# #
# append the Configuration variable to 'make' call with value to use in '/p:Configuration=' # append the Configuration variable to 'make' call with value to use in '/p:Configuration='
# of msbuild command. # of dotnetbuild command.
# #
# append the Flavor variable to 'make' call with value to use in '/p:Flavor=' # append the Flavor variable to 'make' call with value to use in '/p:Flavor='
# of msbuild command. # of dotnetbuild command.
# #
# Example: # Example:
# make Configuration=Release Flavor=NoNet # make Configuration=Release Flavor=NoNet
@@ -18,7 +18,7 @@
# - native: build the native libs # - native: build the native libs
# - java: build the java libs # - java: build the java libs
# - nuget: restore NuGet packages # - nuget: restore NuGet packages
# - msbuild: build the project # - dotnetbuild: build the project
# - apk: same as all # - apk: same as all
# - manifestlink: creates a symlink (to be used in building) to the AndroidManifest corresponding to the selected Flavor # - manifestlink: creates a symlink (to be used in building) to the AndroidManifest corresponding to the selected Flavor
# #
@@ -27,7 +27,7 @@
# - clean_native: clean native lib # - clean_native: clean native lib
# - clean_java: call clean target of java libs # - clean_java: call clean target of java libs
# - clean_nuget: cleanup the 'nuget restore' # - clean_nuget: cleanup the 'nuget restore'
# - clean_msbuild: call clean target of msbuild # - clean_dotnet: call clean target of dotnetbuild
# #
# #
# #
@@ -60,45 +60,23 @@ $(info MAKESHELL: $(MAKESHELL))
$(info SHELL: $(SHELL)) $(info SHELL: $(SHELL))
$(info ) $(info )
# On linux use xabuild, on Windows use MSBuild.exe, otherwise (macos?) use msbuild.
ifeq ($(detected_OS),Linux) ifeq ($(detected_OS),Linux)
MSBUILD_binary := dotnet DOTNET_binary := dotnet
MSBUILD := $(shell $(WHICH) $(MSBUILD_binary)) publish DOTNET := $(shell $(WHICH) $(DOTNET_binary))
else ifeq ($(detected_OS),Windows) else ifeq ($(detected_OS),Windows)
MSBUILD_binary := dotnet DOTNET_binary := dotnet
MSBUILD := $(shell $(WHICH) $(MSBUILD_binary) 2> nul) publish DOTNET := $(shell $(WHICH) $(DOTNET_binary) 2> nul)
ifeq ($(MSBUILD),)
# Additional heuristic to find MSBUILD_BINARY on Windows
VSWHERE := "%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere.exe"
VSWHERE_CHECK := $(shell @echo off & $(VSWHERE) 2> nul || echo VSWHERE_NOT_FOUND)
ifneq ($(VSWHERE_CHECK),VSWHERE_NOT_FOUND)
MSBUILD := $(shell @echo off & $(VSWHERE) -latest -prerelease -products * -requires Microsoft.Component.MSBuild -find MSBuild\**\Bin\MSBuild.exe)
VS_INSTALL_PATH := $(shell @echo off & $(VSWHERE) -property installationPath)
endif
endif
else else
MSBUILD_binary := msbuild DOTNET_binary := dotnet
MSBUILD := $(shell $(WHICH) $(MSBUILD_binary)) DOTNET := $(shell $(WHICH) $(DOTNET_binary))
endif endif
ifeq ($(MSBUILD),) ifeq ($(DOTNET),)
$(info ) $(info )
$(info '$(MSBUILD_binary)' binary could not be found. Check it is in your PATH.) $(info '$(DOTNET_binary)' binary could not be found. Check it is in your PATH.)
ifeq ($(detected_OS),Windows)
ifneq ($(VSWHERE_CHECK),VSWHERE_NOT_FOUND)
$(info )
$(info You may retry after running in the command prompt:)
$(info )
$(info "$(VS_INSTALL_PATH)\VC\Auxiliary\Build\vcvarsall.bat" x86_amd64)
$(info )
$(info If this doesn't work, install/find the location of vcvarsall.bat)
$(info or install and add msbuild.exe to your PATH)
$(info )
endif
endif
$(error ) $(error )
endif endif
$(info MSBUILD: $(MSBUILD)) $(info DOTNET: $(DOTNET))
$(info ) $(info )
ifeq ($(ANDROID_SDK_ROOT),) ifeq ($(ANDROID_SDK_ROOT),)
@@ -117,7 +95,7 @@ endif
$(info ANDROID_NDK_ROOT: $(ANDROID_NDK_ROOT)) $(info ANDROID_NDK_ROOT: $(ANDROID_NDK_ROOT))
ifneq ($(Configuration),) ifneq ($(Configuration),)
MSBUILD_PARAM = -p:Configuration="$(Configuration)" DOTNET_PARAM = -p:Configuration="$(Configuration)"
else else
$(warning Configuration environment variable not set.) $(warning Configuration environment variable not set.)
endif endif
@@ -127,7 +105,7 @@ CREATE_MANIFEST_LINK :=
MANIFEST_FILE := MANIFEST_FILE :=
ifneq ($(Flavor),) ifneq ($(Flavor),)
MSBUILD_PARAM += -p:Flavor="$(Flavor)" DOTNET_PARAM += -p:Flavor="$(Flavor)"
ifneq ($(Flavor),) ifneq ($(Flavor),)
ifeq ($(Flavor),Debug) ifeq ($(Flavor),Debug)
MANIFEST_FILE := AndroidManifest_debug.xml MANIFEST_FILE := AndroidManifest_debug.xml
@@ -152,7 +130,7 @@ else
endif endif
ifneq ($(KeyStore),) ifneq ($(KeyStore),)
MSBUILD_PARAM += -p:AndroidKeyStore=True -p:AndroidSigningKeyStore="$(KeyStore)" -p:AndroidSigningStorePass=env:MyAndroidSigningStorePass -p:AndroidSigningKeyPass=env:MyAndroidSigningKeyPass -p:AndroidSigningKeyAlias="kp2a" DOTNET_PARAM += -p:AndroidKeyStore=True -p:AndroidSigningKeyStore="$(KeyStore)" -p:AndroidSigningStorePass=env:MyAndroidSigningStorePass -p:AndroidSigningKeyPass=env:MyAndroidSigningKeyPass -p:AndroidSigningKeyAlias="kp2a"
endif endif
ifeq ($(detected_OS),Windows) ifeq ($(detected_OS),Windows)
@@ -176,7 +154,7 @@ endif
# Recursive wildcard: https://stackoverflow.com/a/18258352 # Recursive wildcard: https://stackoverflow.com/a/18258352
rwildcard=$(foreach d,$(wildcard $(1:=/*)),$(call rwildcard,$d,$2) $(filter $(subst *,%,$2),$d)) rwildcard=$(foreach d,$(wildcard $(1:=/*)),$(call rwildcard,$d,$2) $(filter $(subst *,%,$2),$d))
$(info MSBUILD_PARAM: $(MSBUILD_PARAM)) $(info DOTNET_PARAM: $(DOTNET_PARAM))
$(info nuget path: $(shell $(WHICH) nuget)) $(info nuget path: $(shell $(WHICH) nuget))
$(info ) $(info )
@@ -254,7 +232,7 @@ OUTPUT_PluginQR = src/java/Keepass2AndroidPluginSDK2/app/build/outputs/aar/Keepa
.PHONY: native $(NATIVE_COMPONENTS) clean_native $(NATIVE_CLEAN_TARGETS) \ .PHONY: native $(NATIVE_COMPONENTS) clean_native $(NATIVE_CLEAN_TARGETS) \
java $(JAVA_COMPONENTS) clean_java $(JAVA_CLEAN_TARGETS) \ java $(JAVA_COMPONENTS) clean_java $(JAVA_CLEAN_TARGETS) \
nuget clean_nuget \ nuget clean_nuget \
msbuild clean_msbuild \ dotnetbuild clean_dotnet \
apk all clean apk all clean
all: apk all: apk
@@ -303,7 +281,7 @@ ifeq ($(shell $(WHICH) nuget),)
endif endif
$(RMFILE) stamp.nuget_* $(RMFILE) stamp.nuget_*
nuget restore src/KeePass.sln nuget restore src/KeePass.sln
$(MSBUILD) src/KeePass.sln -t:restore $(MSBUILD_PARAM) -p:RestorePackagesConfig=true $(DOTNET) restore src/KeePass.sln $(DOTNET_PARAM) -p:RestorePackagesConfig=true
@echo "" > stamp.nuget_$(Flavor) @echo "" > stamp.nuget_$(Flavor)
manifestlink: manifestlink:
@@ -313,20 +291,20 @@ manifestlink:
##### #####
msbuild: manifestlink native java nuget dotnetbuild: manifestlink native java nuget
$(MSBUILD) src/KeePass.sln -target:keepass2android-app -p:AndroidSdkDirectory="$(ANDROID_SDK_ROOT)" -p:BuildProjectReferences=true $(MSBUILD_PARAM) -p:Platform="Any CPU" -m $(DOTNET) build src/KeePass.sln -target:keepass2android-app -p:AndroidSdkDirectory="$(ANDROID_SDK_ROOT)" -p:BuildProjectReferences=true $(DOTNET_PARAM) -p:Platform="Any CPU" -m
apk: msbuild apk: manifestlink native java nuget
$(MSBUILD) src/keepass2android-app/keepass2android-app.csproj -p:AndroidSdkDirectory="$(ANDROID_SDK_ROOT)" -t:SignAndroidPackage $(MSBUILD_PARAM) -p:Platform=AnyCPU -m $(DOTNET) publish src/keepass2android-app/keepass2android-app.csproj -p:AndroidSdkDirectory="$(ANDROID_SDK_ROOT)" -t:SignAndroidPackage $(DOTNET_PARAM) -p:Platform=AnyCPU -m
apk_split: msbuild apk_split: manifestlink native java nuget
$(MSBUILD) src/keepass2android-app/keepass2android-app.csproj -p:AndroidSdkDirectory="$(ANDROID_SDK_ROOT)" -t:SignAndroidPackage $(MSBUILD_PARAM) -p:Platform=AnyCPU -m -p:RuntimeIdentifier=android-arm $(DOTNET) publish src/keepass2android-app/keepass2android-app.csproj -p:AndroidSdkDirectory="$(ANDROID_SDK_ROOT)" -t:SignAndroidPackage $(DOTNET_PARAM) -p:Platform=AnyCPU -m -p:RuntimeIdentifier=android-arm
$(MSBUILD) src/keepass2android-app/keepass2android-app.csproj -p:AndroidSdkDirectory="$(ANDROID_SDK_ROOT)" -t:SignAndroidPackage $(MSBUILD_PARAM) -p:Platform=AnyCPU -m -p:RuntimeIdentifier=android-arm64 $(DOTNET) publish src/keepass2android-app/keepass2android-app.csproj -p:AndroidSdkDirectory="$(ANDROID_SDK_ROOT)" -t:SignAndroidPackage $(DOTNET_PARAM) -p:Platform=AnyCPU -m -p:RuntimeIdentifier=android-arm64
$(MSBUILD) src/keepass2android-app/keepass2android-app.csproj -p:AndroidSdkDirectory="$(ANDROID_SDK_ROOT)" -t:SignAndroidPackage $(MSBUILD_PARAM) -p:Platform=AnyCPU -m -p:RuntimeIdentifier=android-x86 $(DOTNET) publish src/keepass2android-app/keepass2android-app.csproj -p:AndroidSdkDirectory="$(ANDROID_SDK_ROOT)" -t:SignAndroidPackage $(DOTNET_PARAM) -p:Platform=AnyCPU -m -p:RuntimeIdentifier=android-x86
$(MSBUILD) src/keepass2android-app/keepass2android-app.csproj -p:AndroidSdkDirectory="$(ANDROID_SDK_ROOT)" -t:SignAndroidPackage $(MSBUILD_PARAM) -p:Platform=AnyCPU -m -p:RuntimeIdentifier=android-x64 $(DOTNET) publish src/keepass2android-app/keepass2android-app.csproj -p:AndroidSdkDirectory="$(ANDROID_SDK_ROOT)" -t:SignAndroidPackage $(DOTNET_PARAM) -p:Platform=AnyCPU -m -p:RuntimeIdentifier=android-x64
src/build-scripts/rename-output-apks.sh src/keepass2android-app/bin/Release/net8.0-android/ src/build-scripts/rename-output-apks.sh src/keepass2android-app/bin/Release/net8.0-android/
build_all: msbuild build_all: dotnetbuild
##### Cleanup targets ##### Cleanup targets
@@ -370,10 +348,10 @@ else
endif endif
$(RMFILE) stamp.nuget_* $(RMFILE) stamp.nuget_*
clean_msbuild: clean_dotnet:
$(MSBUILD) src/KeePass.sln -target:clean $(MSBUILD_PARAM) $(DOTNET) clean src/KeePass.sln $(DOTNET_PARAM)
clean: clean_native clean_java clean_nuget clean_msbuild clean: clean_native clean_java clean_nuget clean_dotnet
distclean: clean distclean: clean
ifneq ("$(wildcard ./allow_git_clean)","") ifneq ("$(wildcard ./allow_git_clean)","")

View File

@@ -11,10 +11,10 @@ Regular stable releases of Keepass2Android are available on [Google Play](https:
Beta-releases can be obtained by opting in to the [Beta testing channel](https://play.google.com/apps/testing/keepass2android.keepass2android) or [Beta testing channel for Keepass2Android Offline](https://play.google.com/apps/testing/keepass2android.keepass2android_nonet). Beta-releases can be obtained by opting in to the [Beta testing channel](https://play.google.com/apps/testing/keepass2android.keepass2android) or [Beta testing channel for Keepass2Android Offline](https://play.google.com/apps/testing/keepass2android.keepass2android_nonet).
# How can I contribute? # How can I contribute?
* Help to translate Keepass2Android into your language or improve translations at [our Crowdin page](http://crowdin.net/project/keepass2android) * Help to translate Keepass2Android into your language or improve translations at [our Crowdin page](https://crowdin.net/project/keepass2android)
* Add features by [creating a plugin](How-to-create-a-plug-in_.md) or creating a pull request. You might want to contact me before you start working so I can coordinate efforts. * Add features by [creating a plugin](How-to-create-a-plug-in_.md) or creating a pull request. You might want to contact me before you start working so I can coordinate efforts.
* [Become a GitHub sponsor to boost 🚀 development](https://github.com/sponsors/PhilippC) * [Become a GitHub sponsor to boost 🚀 development](https://github.com/sponsors/PhilippC)
* [Make a donation](http://philipp.crocoll.net/donate.php) * [Make a donation](https://philipp.crocoll.net/donate.php)
# How do I learn more? # How do I learn more?
Please see the [wiki](https://github.com/PhilippC/keepass2android/wiki/Documentation) for further information. Please see the [wiki](https://github.com/PhilippC/keepass2android/wiki/Documentation) for further information.

View File

@@ -1,6 +1,6 @@
/* /*
KeePass Password Safe - The Open-Source Password Manager KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2017 Dominik Reichl <dominik.reichl@t-online.de> Copyright (C) 2003-2025 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@@ -40,8 +40,8 @@ namespace KeePassLib.Cryptography
Null = 0, Null = 0,
/// <summary> /// <summary>
/// A variant of the ARCFour algorithm (RC4 incompatible). /// A variant of the ArcFour algorithm (RC4 incompatible).
/// </summary> /// Insecure; for backward compatibility only.
/// </summary> /// </summary>
ArcFourVariant = 1, ArcFourVariant = 1,
@@ -66,68 +66,72 @@ namespace KeePassLib.Cryptography
/// </summary> /// </summary>
public sealed class CryptoRandomStream : IDisposable public sealed class CryptoRandomStream : IDisposable
{ {
private readonly CrsAlgorithm m_crsAlgorithm; private readonly CrsAlgorithm m_alg;
private bool m_bDisposed = false;
private byte[] m_pbState = null; private readonly byte[] m_pbKey = null;
private readonly byte[] m_pbIV = null;
private readonly ChaCha20Cipher m_chacha20 = null;
private readonly Salsa20Cipher m_salsa20 = null;
private readonly byte[] m_pbState = null;
private byte m_i = 0; private byte m_i = 0;
private byte m_j = 0; private byte m_j = 0;
private Salsa20Cipher m_salsa20 = null;
private ChaCha20Cipher m_chacha20 = null;
/// <summary> /// <summary>
/// Construct a new cryptographically secure random stream object. /// Construct a new cryptographically secure random stream object.
/// </summary> /// </summary>
/// <param name="genAlgorithm">Algorithm to use.</param> /// <param name="a">Algorithm to use.</param>
/// <param name="pbKey">Initialization key. Must not be <c>null</c> and /// <param name="pbKey">Initialization key. Must not be <c>null</c>
/// must contain at least 1 byte.</param> /// and must contain at least 1 byte.</param>
public CryptoRandomStream(CrsAlgorithm a, byte[] pbKey) public CryptoRandomStream(CrsAlgorithm a, byte[] pbKey)
{ {
if(pbKey == null) { Debug.Assert(false); throw new ArgumentNullException("pbKey"); } if (pbKey == null) { Debug.Assert(false); throw new ArgumentNullException("pbKey"); }
/// <exception cref="System.ArgumentNullException">Thrown if the
int cbKey = pbKey.Length; int cbKey = pbKey.Length;
if(cbKey <= 0) if (cbKey <= 0)
{ {
Debug.Assert(false); // Need at least one byte Debug.Assert(false); // Need at least one byte
throw new ArgumentOutOfRangeException("pbKey"); throw new ArgumentOutOfRangeException("pbKey");
} }
/// <paramref name="pbKey" /> parameter is <c>null</c>.</exception>
m_crsAlgorithm = a; m_alg = a;
/// <exception cref="System.ArgumentException">Thrown if the
if(a == CrsAlgorithm.ChaCha20) if (a == CrsAlgorithm.ChaCha20)
{ {
byte[] pbKey32 = new byte[32]; m_pbKey = new byte[32];
byte[] pbIV12 = new byte[12]; m_pbIV = new byte[12];
/// <paramref name="pbKey" /> parameter contains no bytes or the
using(SHA512Managed h = new SHA512Managed()) using (SHA512Managed h = new SHA512Managed())
{ {
byte[] pbHash = h.ComputeHash(pbKey); byte[] pbHash = h.ComputeHash(pbKey);
Array.Copy(pbHash, pbKey32, 32); Array.Copy(pbHash, m_pbKey, 32);
Array.Copy(pbHash, 32, pbIV12, 0, 12); Array.Copy(pbHash, 32, m_pbIV, 0, 12);
MemUtil.ZeroByteArray(pbHash); MemUtil.ZeroByteArray(pbHash);
} }
/// algorithm is unknown.</exception>
m_chacha20 = new ChaCha20Cipher(pbKey32, pbIV12, true); m_chacha20 = new ChaCha20Cipher(m_pbKey, m_pbIV, true);
} }
else if(a == CrsAlgorithm.Salsa20) else if (a == CrsAlgorithm.Salsa20)
{ {
byte[] pbKey32 = CryptoUtil.HashSha256(pbKey); m_pbKey = CryptoUtil.HashSha256(pbKey);
byte[] pbIV8 = new byte[8] { 0xE8, 0x30, 0x09, 0x4B, m_pbIV = new byte[8] { 0xE8, 0x30, 0x09, 0x4B,
0x97, 0x20, 0x5D, 0x2A }; // Unique constant 0x97, 0x20, 0x5D, 0x2A }; // Unique constant
m_salsa20 = new Salsa20Cipher(pbKey32, pbIV8); m_salsa20 = new Salsa20Cipher(m_pbKey, m_pbIV);
} }
else if(a == CrsAlgorithm.ArcFourVariant) else if (a == CrsAlgorithm.ArcFourVariant)
{ {
// Fill the state linearly // Fill the state linearly
m_pbState = new byte[256]; m_pbState = new byte[256];
for(int w = 0; w < 256; ++w) m_pbState[w] = (byte)w; for (int w = 0; w < 256; ++w) m_pbState[w] = (byte)w;
unchecked unchecked
{ {
byte j = 0, t; byte j = 0, t;
int inxKey = 0; int inxKey = 0;
for(int w = 0; w < 256; ++w) // Key setup for (int w = 0; w < 256; ++w) // Key setup
{ {
j += (byte)(m_pbState[w] + pbKey[inxKey]); j += (byte)(m_pbState[w] + pbKey[inxKey]);
@@ -136,7 +140,7 @@ namespace KeePassLib.Cryptography
m_pbState[j] = t; m_pbState[j] = t;
++inxKey; ++inxKey;
if(inxKey >= cbKey) inxKey = 0; if (inxKey >= cbKey) inxKey = 0;
} }
} }
@@ -157,19 +161,24 @@ namespace KeePassLib.Cryptography
private void Dispose(bool disposing) private void Dispose(bool disposing)
{ {
if(disposing) if (disposing)
{ {
if(m_crsAlgorithm == CrsAlgorithm.ChaCha20) if (m_alg == CrsAlgorithm.ChaCha20)
m_chacha20.Dispose(); m_chacha20.Dispose();
else if(m_crsAlgorithm == CrsAlgorithm.Salsa20) else if (m_alg == CrsAlgorithm.Salsa20)
m_salsa20.Dispose(); m_salsa20.Dispose();
else if(m_crsAlgorithm == CrsAlgorithm.ArcFourVariant) else if (m_alg == CrsAlgorithm.ArcFourVariant)
{ {
MemUtil.ZeroByteArray(m_pbState); MemUtil.ZeroByteArray(m_pbState);
m_i = 0; m_i = 0;
m_j = 0; m_j = 0;
} }
else { Debug.Assert(false); } else { Debug.Assert(false); }
if (m_pbKey != null) MemUtil.ZeroByteArray(m_pbKey);
if (m_pbIV != null) MemUtil.ZeroByteArray(m_pbIV);
m_bDisposed = true;
} }
} }
@@ -180,23 +189,24 @@ namespace KeePassLib.Cryptography
/// <returns>Returns <paramref name="uRequestedCount" /> random bytes.</returns> /// <returns>Returns <paramref name="uRequestedCount" /> random bytes.</returns>
public byte[] GetRandomBytes(uint uRequestedCount) public byte[] GetRandomBytes(uint uRequestedCount)
{ {
if(uRequestedCount == 0) return MemUtil.EmptyByteArray; if (m_bDisposed) throw new ObjectDisposedException(null);
if(uRequestedCount > (uint)int.MaxValue) if (uRequestedCount == 0) return MemUtil.EmptyByteArray;
if (uRequestedCount > (uint)int.MaxValue)
throw new ArgumentOutOfRangeException("uRequestedCount"); throw new ArgumentOutOfRangeException("uRequestedCount");
int cb = (int)uRequestedCount; int cb = (int)uRequestedCount;
byte[] pbRet = new byte[cb]; byte[] pbRet = new byte[cb];
if(m_crsAlgorithm == CrsAlgorithm.ChaCha20) if (m_alg == CrsAlgorithm.ChaCha20)
m_chacha20.Encrypt(pbRet, 0, cb); m_chacha20.Encrypt(pbRet, 0, cb);
else if(m_crsAlgorithm == CrsAlgorithm.Salsa20) else if (m_alg == CrsAlgorithm.Salsa20)
m_salsa20.Encrypt(pbRet, 0, cb); m_salsa20.Encrypt(pbRet, 0, cb);
else if(m_crsAlgorithm == CrsAlgorithm.ArcFourVariant) else if (m_alg == CrsAlgorithm.ArcFourVariant)
{ {
unchecked unchecked
{ {
for(int w = 0; w < cb; ++w) for (int w = 0; w < cb; ++w)
{ {
++m_i; ++m_i;
m_j += m_pbState[m_i]; m_j += m_pbState[m_i];
@@ -221,6 +231,25 @@ namespace KeePassLib.Cryptography
return MemUtil.BytesToUInt64(pb); return MemUtil.BytesToUInt64(pb);
} }
internal ulong GetRandomUInt64(ulong uMaxExcl)
{
if (uMaxExcl == 0) { Debug.Assert(false); throw new ArgumentOutOfRangeException("uMaxExcl"); }
ulong uGen, uRem;
do
{
uGen = GetRandomUInt64();
uRem = uGen % uMaxExcl;
}
while ((uGen - uRem) > (ulong.MaxValue - (uMaxExcl - 1UL)));
// This ensures that the last number of the block (i.e.
// (uGen - uRem) + (uMaxExcl - 1)) is generatable;
// for signed longs, overflow to negative number:
// while((uGen - uRem) + (uMaxExcl - 1) < 0);
return uRem;
}
#if CRSBENCHMARK #if CRSBENCHMARK
public static string Benchmark() public static string Benchmark()
{ {
@@ -237,21 +266,20 @@ namespace KeePassLib.Cryptography
return str; return str;
} }
private static int BenchTime(CrsAlgorithm cra, int nRounds, int nDataSize) private static int BenchTime(CrsAlgorithm a, int nRounds, int cbData)
{ {
byte[] pbKey = new byte[4] { 0x00, 0x01, 0x02, 0x03 }; byte[] pbKey = new byte[4] { 0x00, 0x01, 0x02, 0x03 };
int nStart = Environment.TickCount; int tStart = Environment.TickCount;
for(int i = 0; i < nRounds; ++i) for(int i = 0; i < nRounds; ++i)
{ {
using(CryptoRandomStream c = new CryptoRandomStream(cra, pbKey)) using(CryptoRandomStream crs = new CryptoRandomStream(a, pbKey))
{ {
c.GetRandomBytes((uint)nDataSize); crs.GetRandomBytes((uint)cbData);
} }
} }
int nEnd = Environment.TickCount;
return (nEnd - nStart); return (Environment.TickCount - tStart);
} }
#endif #endif
} }

View File

@@ -1,65 +0,0 @@
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2017 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using KeePassLib.Security;
using KeePassLib.Utility;
namespace KeePassLib.Cryptography.PasswordGenerator
{
internal static class CharSetBasedGenerator
{
internal static PwgError Generate(out ProtectedString psOut,
PwProfile pwProfile, CryptoRandomStream crsRandomSource)
{
psOut = ProtectedString.Empty;
if(pwProfile.Length == 0) return PwgError.Success;
PwCharSet pcs = new PwCharSet(pwProfile.CharSet.ToString());
char[] vGenerated = new char[pwProfile.Length];
PwGenerator.PrepareCharSet(pcs, pwProfile);
for(int nIndex = 0; nIndex < (int)pwProfile.Length; ++nIndex)
{
char ch = PwGenerator.GenerateCharacter(pwProfile, pcs,
crsRandomSource);
if(ch == char.MinValue)
{
MemUtil.ZeroArray<char>(vGenerated);
return PwgError.TooFewCharacters;
}
vGenerated[nIndex] = ch;
}
byte[] pbUtf8 = StrUtil.Utf8.GetBytes(vGenerated);
psOut = new ProtectedString(true, pbUtf8);
MemUtil.ZeroByteArray(pbUtf8);
MemUtil.ZeroArray<char>(vGenerated);
return PwgError.Success;
}
}
}

View File

@@ -1,173 +0,0 @@
/*
KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2017 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using KeePassLib.Security;
using KeePassLib.Utility;
namespace KeePassLib.Cryptography.PasswordGenerator
{
internal static class PatternBasedGenerator
{
internal static PwgError Generate(out ProtectedString psOut,
PwProfile pwProfile, CryptoRandomStream crsRandomSource)
{
psOut = ProtectedString.Empty;
LinkedList<char> vGenerated = new LinkedList<char>();
PwCharSet pcsCurrent = new PwCharSet();
PwCharSet pcsCustom = new PwCharSet();
PwCharSet pcsUsed = new PwCharSet();
bool bInCharSetDef = false;
string strPattern = ExpandPattern(pwProfile.Pattern);
if(strPattern.Length == 0) return PwgError.Success;
CharStream csStream = new CharStream(strPattern);
char ch = csStream.ReadChar();
while(ch != char.MinValue)
{
pcsCurrent.Clear();
bool bGenerateChar = false;
if(ch == '\\')
{
ch = csStream.ReadChar();
if(ch == char.MinValue) // Backslash at the end
{
vGenerated.AddLast('\\');
break;
}
if(bInCharSetDef) pcsCustom.Add(ch);
else
{
vGenerated.AddLast(ch);
pcsUsed.Add(ch);
}
}
else if(ch == '[')
{
pcsCustom.Clear();
bInCharSetDef = true;
}
else if(ch == ']')
{
pcsCurrent.Add(pcsCustom.ToString());
bInCharSetDef = false;
bGenerateChar = true;
}
else if(bInCharSetDef)
{
if(pcsCustom.AddCharSet(ch) == false)
pcsCustom.Add(ch);
}
else if(pcsCurrent.AddCharSet(ch) == false)
{
vGenerated.AddLast(ch);
pcsUsed.Add(ch);
}
else bGenerateChar = true;
if(bGenerateChar)
{
PwGenerator.PrepareCharSet(pcsCurrent, pwProfile);
if(pwProfile.NoRepeatingCharacters)
pcsCurrent.Remove(pcsUsed.ToString());
char chGen = PwGenerator.GenerateCharacter(pwProfile,
pcsCurrent, crsRandomSource);
if(chGen == char.MinValue) return PwgError.TooFewCharacters;
vGenerated.AddLast(chGen);
pcsUsed.Add(chGen);
}
ch = csStream.ReadChar();
}
if(vGenerated.Count == 0) return PwgError.Success;
char[] vArray = new char[vGenerated.Count];
vGenerated.CopyTo(vArray, 0);
if(pwProfile.PatternPermutePassword)
PwGenerator.ShufflePassword(vArray, crsRandomSource);
byte[] pbUtf8 = StrUtil.Utf8.GetBytes(vArray);
psOut = new ProtectedString(true, pbUtf8);
MemUtil.ZeroByteArray(pbUtf8);
MemUtil.ZeroArray<char>(vArray);
vGenerated.Clear();
return PwgError.Success;
}
private static string ExpandPattern(string strPattern)
{
Debug.Assert(strPattern != null); if(strPattern == null) return string.Empty;
string str = strPattern;
while(true)
{
int nOpen = FindFirstUnescapedChar(str, '{');
int nClose = FindFirstUnescapedChar(str, '}');
if((nOpen >= 0) && (nOpen < nClose))
{
string strCount = str.Substring(nOpen + 1, nClose - nOpen - 1);
str = str.Remove(nOpen, nClose - nOpen + 1);
uint uRepeat;
if(StrUtil.TryParseUInt(strCount, out uRepeat) && (nOpen >= 1))
{
if(uRepeat == 0)
str = str.Remove(nOpen - 1, 1);
else
str = str.Insert(nOpen, new string(str[nOpen - 1], (int)uRepeat - 1));
}
}
else break;
}
return str;
}
private static int FindFirstUnescapedChar(string str, char ch)
{
for(int i = 0; i < str.Length; ++i)
{
char chCur = str[i];
if(chCur == '\\') ++i; // Next is escaped, skip it
else if(chCur == ch) return i;
}
return -1;
}
}
}

View File

@@ -1,6 +1,6 @@
/* /*
KeePass Password Safe - The Open-Source Password Manager KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2017 Dominik Reichl <dominik.reichl@t-online.de> Copyright (C) 2003-2025 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@@ -19,122 +19,81 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text;
using System.Diagnostics; using System.Diagnostics;
using System.Text;
using KeePassLib.Utility;
namespace KeePassLib.Cryptography.PasswordGenerator namespace KeePassLib.Cryptography.PasswordGenerator
{ {
public sealed class PwCharSet public sealed class PwCharSet : IEquatable<PwCharSet>
{ {
public const string UpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; public static readonly string UpperCase = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
public const string LowerCase = "abcdefghijklmnopqrstuvwxyz"; public static readonly string LowerCase = "abcdefghijklmnopqrstuvwxyz";
public const string Digits = "0123456789"; public static readonly string Digits = "0123456789";
public const string UpperConsonants = "BCDFGHJKLMNPQRSTVWXYZ"; public static readonly string UpperConsonants = "BCDFGHJKLMNPQRSTVWXYZ";
public const string LowerConsonants = "bcdfghjklmnpqrstvwxyz"; public static readonly string LowerConsonants = "bcdfghjklmnpqrstvwxyz";
public const string UpperVowels = "AEIOU"; public static readonly string UpperVowels = "AEIOU";
public const string LowerVowels = "aeiou"; public static readonly string LowerVowels = "aeiou";
public const string Punctuation = @",.;:"; public static readonly string Punctuation = ",.;:";
public const string Brackets = @"[]{}()<>"; public static readonly string Brackets = @"[]{}()<>";
public const string PrintableAsciiSpecial = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"; public static readonly string Special = "!\"#$%&'*+,./:;=?@\\^`|~";
public static readonly string PrintableAsciiSpecial = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
public const string UpperHex = "0123456789ABCDEF"; public static readonly string UpperHex = "0123456789ABCDEF";
public const string LowerHex = "0123456789abcdef"; public static readonly string LowerHex = "0123456789abcdef";
public const string Invalid = "\t\r\n"; public static readonly string LookAlike = "O0Il1|";
public const string LookAlike = @"O0l1I|";
internal const string MenuAccels = PwCharSet.LowerCase + PwCharSet.Digits;
private const int CharTabSize = (0x10000 / 8);
private List<char> m_vChars = new List<char>();
private byte[] m_vTab = new byte[CharTabSize];
private static string m_strHighAnsi = null;
public static string HighAnsiChars
{
get
{
if(m_strHighAnsi == null) { new PwCharSet(); } // Create string
Debug.Assert(m_strHighAnsi != null);
return m_strHighAnsi;
}
}
private static string m_strSpecial = null;
public static string SpecialChars
{
get
{
if(m_strSpecial == null) { new PwCharSet(); } // Create string
Debug.Assert(m_strSpecial != null);
return m_strSpecial;
}
}
/// <summary> /// <summary>
/// Create a new, empty character set collection object. /// Latin-1 Supplement except U+00A0 (NBSP) and U+00AD (SHY).
/// </summary>
public static readonly string Latin1S =
"\u00A1\u00A2\u00A3\u00A4\u00A5\u00A6\u00A7" +
"\u00A8\u00A9\u00AA\u00AB\u00AC\u00AE\u00AF" +
"\u00B0\u00B1\u00B2\u00B3\u00B4\u00B5\u00B6\u00B7" +
"\u00B8\u00B9\u00BA\u00BB\u00BC\u00BD\u00BE\u00BF" +
"\u00C0\u00C1\u00C2\u00C3\u00C4\u00C5\u00C6\u00C7" +
"\u00C8\u00C9\u00CA\u00CB\u00CC\u00CD\u00CE\u00CF" +
"\u00D0\u00D1\u00D2\u00D3\u00D4\u00D5\u00D6\u00D7" +
"\u00D8\u00D9\u00DA\u00DB\u00DC\u00DD\u00DE\u00DF" +
"\u00E0\u00E1\u00E2\u00E3\u00E4\u00E5\u00E6\u00E7" +
"\u00E8\u00E9\u00EA\u00EB\u00EC\u00ED\u00EE\u00EF" +
"\u00F0\u00F1\u00F2\u00F3\u00F4\u00F5\u00F6\u00F7" +
"\u00F8\u00F9\u00FA\u00FB\u00FC\u00FD\u00FE\u00FF";
// internal static readonly string MenuAccels = PwCharSet.LowerCase + PwCharSet.Digits;
[Obsolete]
public static string SpecialChars { get { return PwCharSet.Special; } }
[Obsolete]
public static string HighAnsiChars { get { return PwCharSet.Latin1S; } }
private readonly List<char> m_lChars = new List<char>();
private readonly byte[] m_vTab = new byte[0x10000 / 8];
/// <summary>
/// Create a new, empty character set.
/// </summary> /// </summary>
public PwCharSet() public PwCharSet()
{ {
Initialize(true); Debug.Assert(PwCharSet.Latin1S.Length == (16 * 6 - 2));
} }
public PwCharSet(string strCharSet) public PwCharSet(string strCharSet)
{ {
Initialize(true);
Add(strCharSet); Add(strCharSet);
} }
private PwCharSet(bool bFullInitialize)
{
Initialize(bFullInitialize);
}
private void Initialize(bool bFullInitialize)
{
Clear();
if(!bFullInitialize) return;
if(m_strHighAnsi == null)
{
StringBuilder sbHighAnsi = new StringBuilder();
// [U+0080, U+009F] are C1 control characters,
// U+00A0 is non-breaking space
for(char ch = '\u00A1'; ch <= '\u00AC'; ++ch)
sbHighAnsi.Append(ch);
// U+00AD is soft hyphen (format character)
for(char ch = '\u00AE'; ch < '\u00FF'; ++ch)
sbHighAnsi.Append(ch);
sbHighAnsi.Append('\u00FF');
m_strHighAnsi = sbHighAnsi.ToString();
}
if(m_strSpecial == null)
{
PwCharSet pcs = new PwCharSet(false);
pcs.AddRange('!', '/');
pcs.AddRange(':', '@');
pcs.AddRange('[', '`');
pcs.Add(@"|~");
pcs.Remove(@"-_ ");
pcs.Remove(PwCharSet.Brackets);
m_strSpecial = pcs.ToString();
}
}
/// <summary> /// <summary>
/// Number of characters in this set. /// Number of characters in this set.
/// </summary> /// </summary>
public uint Size public uint Size
{ {
get { return (uint)m_vChars.Count; } get { return (uint)m_lChars.Count; }
} }
/// <summary> /// <summary>
@@ -147,19 +106,39 @@ namespace KeePassLib.Cryptography.PasswordGenerator
{ {
get get
{ {
if(uPos >= (uint)m_vChars.Count) if (uPos >= (uint)m_lChars.Count)
throw new ArgumentOutOfRangeException("uPos"); throw new ArgumentOutOfRangeException("uPos");
return m_vChars[(int)uPos]; return m_lChars[(int)uPos];
} }
} }
public bool Equals(PwCharSet other)
{
if (object.ReferenceEquals(other, this)) return true;
if (object.ReferenceEquals(other, null)) return false;
if (m_lChars.Count != other.m_lChars.Count) return false;
return MemUtil.ArraysEqual(m_vTab, other.m_vTab);
}
public override bool Equals(object obj)
{
return Equals(obj as PwCharSet);
}
public override int GetHashCode()
{
return (int)MemUtil.Hash32(m_vTab, 0, m_vTab.Length);
}
/// <summary> /// <summary>
/// Remove all characters from this set. /// Remove all characters from this set.
/// </summary> /// </summary>
public void Clear() public void Clear()
{ {
m_vChars.Clear(); m_lChars.Clear();
Array.Clear(m_vTab, 0, m_vTab.Length); Array.Clear(m_vTab, 0, m_vTab.Length);
} }
@@ -171,11 +150,11 @@ namespace KeePassLib.Cryptography.PasswordGenerator
public bool Contains(string strCharacters) public bool Contains(string strCharacters)
{ {
Debug.Assert(strCharacters != null); Debug.Assert(strCharacters != null);
if(strCharacters == null) throw new ArgumentNullException("strCharacters"); if (strCharacters == null) throw new ArgumentNullException("strCharacters");
foreach(char ch in strCharacters) foreach (char ch in strCharacters)
{ {
if(!Contains(ch)) return false; if (!Contains(ch)) return false;
} }
return true; return true;
@@ -187,11 +166,11 @@ namespace KeePassLib.Cryptography.PasswordGenerator
/// <param name="ch">Character to add.</param> /// <param name="ch">Character to add.</param>
public void Add(char ch) public void Add(char ch)
{ {
if(ch == char.MinValue) { Debug.Assert(false); return; } if (ch == char.MinValue) { Debug.Assert(false); return; }
if(!Contains(ch)) if (!Contains(ch))
{ {
m_vChars.Add(ch); m_lChars.Add(ch);
m_vTab[ch / 8] |= (byte)(1 << (ch % 8)); m_vTab[ch / 8] |= (byte)(1 << (ch % 8));
} }
} }
@@ -203,11 +182,9 @@ namespace KeePassLib.Cryptography.PasswordGenerator
public void Add(string strCharSet) public void Add(string strCharSet)
{ {
Debug.Assert(strCharSet != null); Debug.Assert(strCharSet != null);
if(strCharSet == null) throw new ArgumentNullException("strCharSet"); if (strCharSet == null) throw new ArgumentNullException("strCharSet");
m_vChars.Capacity = m_vChars.Count + strCharSet.Length; foreach (char ch in strCharSet)
foreach(char ch in strCharSet)
Add(ch); Add(ch);
} }
@@ -226,9 +203,7 @@ namespace KeePassLib.Cryptography.PasswordGenerator
public void AddRange(char chMin, char chMax) public void AddRange(char chMin, char chMax)
{ {
m_vChars.Capacity = m_vChars.Count + (chMax - chMin) + 1; for (char ch = chMin; ch < chMax; ++ch)
for(char ch = chMin; ch < chMax; ++ch)
Add(ch); Add(ch);
Add(chMax); Add(chMax);
@@ -238,14 +213,16 @@ namespace KeePassLib.Cryptography.PasswordGenerator
{ {
bool bResult = true; bool bResult = true;
switch(chCharSetIdentifier) switch (chCharSetIdentifier)
{ {
case 'a': Add(PwCharSet.LowerCase, PwCharSet.Digits); break; case 'a': Add(PwCharSet.LowerCase, PwCharSet.Digits); break;
case 'A': Add(PwCharSet.LowerCase, PwCharSet.UpperCase, case 'A':
Add(PwCharSet.LowerCase, PwCharSet.UpperCase,
PwCharSet.Digits); break; PwCharSet.Digits); break;
case 'U': Add(PwCharSet.UpperCase, PwCharSet.Digits); break; case 'U': Add(PwCharSet.UpperCase, PwCharSet.Digits); break;
case 'c': Add(PwCharSet.LowerConsonants); break; case 'c': Add(PwCharSet.LowerConsonants); break;
case 'C': Add(PwCharSet.LowerConsonants, case 'C':
Add(PwCharSet.LowerConsonants,
PwCharSet.UpperConsonants); break; PwCharSet.UpperConsonants); break;
case 'z': Add(PwCharSet.UpperConsonants); break; case 'z': Add(PwCharSet.UpperConsonants); break;
case 'd': Add(PwCharSet.Digits); break; // Digit case 'd': Add(PwCharSet.Digits); break; // Digit
@@ -257,12 +234,13 @@ namespace KeePassLib.Cryptography.PasswordGenerator
case 'p': Add(PwCharSet.Punctuation); break; case 'p': Add(PwCharSet.Punctuation); break;
case 'b': Add(PwCharSet.Brackets); break; case 'b': Add(PwCharSet.Brackets); break;
case 's': Add(PwCharSet.PrintableAsciiSpecial); break; case 's': Add(PwCharSet.PrintableAsciiSpecial); break;
case 'S': Add(PwCharSet.UpperCase, PwCharSet.LowerCase); case 'S':
Add(PwCharSet.UpperCase, PwCharSet.LowerCase);
Add(PwCharSet.Digits, PwCharSet.PrintableAsciiSpecial); break; Add(PwCharSet.Digits, PwCharSet.PrintableAsciiSpecial); break;
case 'v': Add(PwCharSet.LowerVowels); break; case 'v': Add(PwCharSet.LowerVowels); break;
case 'V': Add(PwCharSet.LowerVowels, PwCharSet.UpperVowels); break; case 'V': Add(PwCharSet.LowerVowels, PwCharSet.UpperVowels); break;
case 'Z': Add(PwCharSet.UpperVowels); break; case 'Z': Add(PwCharSet.UpperVowels); break;
case 'x': Add(m_strHighAnsi); break; case 'x': Add(PwCharSet.Latin1S); break;
default: bResult = false; break; default: bResult = false; break;
} }
@@ -272,18 +250,18 @@ namespace KeePassLib.Cryptography.PasswordGenerator
public bool Remove(char ch) public bool Remove(char ch)
{ {
m_vTab[ch / 8] &= (byte)(~(1 << (ch % 8))); m_vTab[ch / 8] &= (byte)(~(1 << (ch % 8)));
return m_vChars.Remove(ch); return m_lChars.Remove(ch);
} }
public bool Remove(string strCharacters) public bool Remove(string strCharacters)
{ {
Debug.Assert(strCharacters != null); Debug.Assert(strCharacters != null);
if(strCharacters == null) throw new ArgumentNullException("strCharacters"); if (strCharacters == null) throw new ArgumentNullException("strCharacters");
bool bResult = true; bool bResult = true;
foreach(char ch in strCharacters) foreach (char ch in strCharacters)
{ {
if(!Remove(ch)) bResult = false; if (!Remove(ch)) bResult = false;
} }
return bResult; return bResult;
@@ -292,9 +270,9 @@ namespace KeePassLib.Cryptography.PasswordGenerator
public bool RemoveIfAllExist(string strCharacters) public bool RemoveIfAllExist(string strCharacters)
{ {
Debug.Assert(strCharacters != null); Debug.Assert(strCharacters != null);
if(strCharacters == null) throw new ArgumentNullException("strCharacters"); if (strCharacters == null) throw new ArgumentNullException("strCharacters");
if(!Contains(strCharacters)) if (!Contains(strCharacters))
return false; return false;
return Remove(strCharacters); return Remove(strCharacters);
@@ -306,8 +284,8 @@ namespace KeePassLib.Cryptography.PasswordGenerator
/// <returns>String containing all character set characters.</returns> /// <returns>String containing all character set characters.</returns>
public override string ToString() public override string ToString()
{ {
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder(m_lChars.Count);
foreach(char ch in m_vChars) foreach (char ch in m_lChars)
sb.Append(ch); sb.Append(ch);
return sb.ToString(); return sb.ToString();
@@ -320,32 +298,32 @@ namespace KeePassLib.Cryptography.PasswordGenerator
sb.Append(RemoveIfAllExist(PwCharSet.UpperCase) ? 'U' : '_'); sb.Append(RemoveIfAllExist(PwCharSet.UpperCase) ? 'U' : '_');
sb.Append(RemoveIfAllExist(PwCharSet.LowerCase) ? 'L' : '_'); sb.Append(RemoveIfAllExist(PwCharSet.LowerCase) ? 'L' : '_');
sb.Append(RemoveIfAllExist(PwCharSet.Digits) ? 'D' : '_'); sb.Append(RemoveIfAllExist(PwCharSet.Digits) ? 'D' : '_');
sb.Append(RemoveIfAllExist(m_strSpecial) ? 'S' : '_'); sb.Append(RemoveIfAllExist(PwCharSet.Special) ? 'S' : '_');
sb.Append(RemoveIfAllExist(PwCharSet.Punctuation) ? 'P' : '_'); sb.Append(RemoveIfAllExist(PwCharSet.Punctuation) ? 'P' : '_');
sb.Append(RemoveIfAllExist(@"-") ? 'm' : '_'); sb.Append(RemoveIfAllExist("-") ? 'm' : '_');
sb.Append(RemoveIfAllExist(@"_") ? 'u' : '_'); sb.Append(RemoveIfAllExist("_") ? 'u' : '_');
sb.Append(RemoveIfAllExist(@" ") ? 's' : '_'); sb.Append(RemoveIfAllExist(" ") ? 's' : '_');
sb.Append(RemoveIfAllExist(PwCharSet.Brackets) ? 'B' : '_'); sb.Append(RemoveIfAllExist(PwCharSet.Brackets) ? 'B' : '_');
sb.Append(RemoveIfAllExist(m_strHighAnsi) ? 'H' : '_'); sb.Append(RemoveIfAllExist(PwCharSet.Latin1S) ? 'H' : '_');
return sb.ToString(); return sb.ToString();
} }
public void UnpackCharRanges(string strRanges) public void UnpackCharRanges(string strRanges)
{ {
if(strRanges == null) { Debug.Assert(false); return; } if (strRanges == null) { Debug.Assert(false); return; }
if(strRanges.Length < 10) { Debug.Assert(false); return; } if (strRanges.Length < 10) { Debug.Assert(false); return; }
if(strRanges[0] != '_') Add(PwCharSet.UpperCase); if (strRanges[0] != '_') Add(PwCharSet.UpperCase);
if(strRanges[1] != '_') Add(PwCharSet.LowerCase); if (strRanges[1] != '_') Add(PwCharSet.LowerCase);
if(strRanges[2] != '_') Add(PwCharSet.Digits); if (strRanges[2] != '_') Add(PwCharSet.Digits);
if(strRanges[3] != '_') Add(m_strSpecial); if (strRanges[3] != '_') Add(PwCharSet.Special);
if(strRanges[4] != '_') Add(PwCharSet.Punctuation); if (strRanges[4] != '_') Add(PwCharSet.Punctuation);
if(strRanges[5] != '_') Add('-'); if (strRanges[5] != '_') Add('-');
if(strRanges[6] != '_') Add('_'); if (strRanges[6] != '_') Add('_');
if(strRanges[7] != '_') Add(' '); if (strRanges[7] != '_') Add(' ');
if(strRanges[8] != '_') Add(PwCharSet.Brackets); if (strRanges[8] != '_') Add(PwCharSet.Brackets);
if(strRanges[9] != '_') Add(m_strHighAnsi); if (strRanges[9] != '_') Add(PwCharSet.Latin1S);
} }
} }
} }

View File

@@ -1,6 +1,6 @@
/* /*
KeePass Password Safe - The Open-Source Password Manager KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2016 Dominik Reichl <dominik.reichl@t-online.de> Copyright (C) 2003-2025 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@@ -20,9 +20,13 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Security.Cryptography;
using System.Text; using System.Text;
#if !KeePassUAP
using System.Security.Cryptography;
#endif
using KeePassLib.Resources;
using KeePassLib.Security; using KeePassLib.Security;
using KeePassLib.Utility; using KeePassLib.Utility;
@@ -33,95 +37,78 @@ namespace KeePassLib.Cryptography.PasswordGenerator
Success = 0, Success = 0,
Unknown = 1, Unknown = 1,
TooFewCharacters = 2, TooFewCharacters = 2,
UnknownAlgorithm = 3 UnknownAlgorithm = 3,
InvalidCharSet = 4,
InvalidPattern = 5
} }
/// <summary> /// <summary>
/// Utility functions for generating random passwords. /// Password generator.
/// </summary> /// </summary>
public static class PwGenerator public static class PwGenerator
{ {
public static PwgError Generate(out ProtectedString psOut,
PwProfile pwProfile, byte[] pbUserEntropy, private static CryptoRandomStream CreateRandomStream(byte[] pbAdditionalEntropy,
CustomPwGeneratorPool pwAlgorithmPool) out byte[] pbKey)
{ {
Debug.Assert(pwProfile != null); pbKey = CryptoRandom.Instance.GetRandomBytes(128);
if (pwProfile == null) throw new ArgumentNullException("pwProfile");
CryptoRandomStream crs = CreateCryptoStream(pbUserEntropy);
PwgError e = PwgError.Unknown;
if (pwProfile.GeneratorType == PasswordGeneratorType.CharSet)
e = CharSetBasedGenerator.Generate(out psOut, pwProfile, crs);
else if (pwProfile.GeneratorType == PasswordGeneratorType.Pattern)
e = PatternBasedGenerator.Generate(out psOut, pwProfile, crs);
else if (pwProfile.GeneratorType == PasswordGeneratorType.Custom)
e = GenerateCustom(out psOut, pwProfile, crs, pwAlgorithmPool);
else { Debug.Assert(false); psOut = ProtectedString.Empty; }
return e;
}
private static CryptoRandomStream CreateCryptoStream(byte[] pbAdditionalEntropy)
{
byte[] pbKey = CryptoRandom.Instance.GetRandomBytes(128);
// Mix in additional entropy // Mix in additional entropy
Debug.Assert(pbKey.Length >= 64); Debug.Assert(pbKey.Length >= 64);
if ((pbAdditionalEntropy != null) && (pbAdditionalEntropy.Length > 0)) if ((pbAdditionalEntropy != null) && (pbAdditionalEntropy.Length != 0))
{ {
using (SHA512Managed h = new SHA512Managed()) using (SHA512Managed h = new SHA512Managed())
{ {
byte[] pbHash = h.ComputeHash(pbAdditionalEntropy); byte[] pbHash = h.ComputeHash(pbAdditionalEntropy);
MemUtil.XorArray(pbHash, 0, pbKey, 0, pbHash.Length); MemUtil.XorArray(pbHash, 0, pbKey, 0, pbHash.Length);
MemUtil.ZeroByteArray(pbHash);
} }
} }
return new CryptoRandomStream(CrsAlgorithm.ChaCha20, pbKey); return new CryptoRandomStream(CrsAlgorithm.ChaCha20, pbKey);
} }
internal static char GenerateCharacter(PwProfile pwProfile, internal static char GenerateCharacter(PwCharSet pwCharSet,
PwCharSet pwCharSet, CryptoRandomStream crsRandomSource) CryptoRandomStream crsRandomSource)
{ {
if (pwCharSet.Size == 0) return char.MinValue; uint cc = pwCharSet.Size;
if (cc == 0) return char.MinValue;
ulong uIndex = crsRandomSource.GetRandomUInt64(); uint i = (uint)crsRandomSource.GetRandomUInt64(cc);
uIndex %= (ulong)pwCharSet.Size; return pwCharSet[i];
char ch = pwCharSet[(uint)uIndex];
if (pwProfile.NoRepeatingCharacters)
pwCharSet.Remove(ch);
return ch;
} }
internal static void PrepareCharSet(PwCharSet pwCharSet, PwProfile pwProfile) internal static bool PrepareCharSet(PwCharSet pwCharSet, PwProfile pwProfile)
{ {
pwCharSet.Remove(PwCharSet.Invalid); uint cc = pwCharSet.Size;
for (uint i = 0; i < cc; ++i)
{
char ch = pwCharSet[i];
if ((ch == char.MinValue) || (ch == '\t') || (ch == '\r') ||
(ch == '\n') || char.IsSurrogate(ch))
return false;
}
if (pwProfile.ExcludeLookAlike) pwCharSet.Remove(PwCharSet.LookAlike); if (pwProfile.ExcludeLookAlike) pwCharSet.Remove(PwCharSet.LookAlike);
if (pwProfile.ExcludeCharacters.Length > 0) if (!string.IsNullOrEmpty(pwProfile.ExcludeCharacters))
pwCharSet.Remove(pwProfile.ExcludeCharacters); pwCharSet.Remove(pwProfile.ExcludeCharacters);
return true;
} }
internal static void ShufflePassword(char[] pPassword, internal static void Shuffle(char[] v, CryptoRandomStream crsRandomSource)
CryptoRandomStream crsRandomSource)
{ {
Debug.Assert(pPassword != null); if (pPassword == null) return; if (v == null) { Debug.Assert(false); return; }
Debug.Assert(crsRandomSource != null); if (crsRandomSource == null) return; if (crsRandomSource == null) { Debug.Assert(false); return; }
if (pPassword.Length <= 1) return; // Nothing to shuffle for (int i = v.Length - 1; i >= 1; --i)
for (int nSelect = 0; nSelect < pPassword.Length; ++nSelect)
{ {
ulong uRandomIndex = crsRandomSource.GetRandomUInt64(); int j = (int)crsRandomSource.GetRandomUInt64((ulong)(i + 1));
uRandomIndex %= (ulong)(pPassword.Length - nSelect);
char chTemp = pPassword[nSelect]; char t = v[i];
pPassword[nSelect] = pPassword[nSelect + (int)uRandomIndex]; v[i] = v[j];
pPassword[nSelect + (int)uRandomIndex] = chTemp; v[j] = t;
} }
} }
@@ -135,7 +122,7 @@ namespace KeePassLib.Cryptography.PasswordGenerator
if (pwAlgorithmPool == null) return PwgError.UnknownAlgorithm; if (pwAlgorithmPool == null) return PwgError.UnknownAlgorithm;
string strID = pwProfile.CustomAlgorithmUuid; string strID = pwProfile.CustomAlgorithmUuid;
if (string.IsNullOrEmpty(strID)) { Debug.Assert(false); return PwgError.UnknownAlgorithm; } if (string.IsNullOrEmpty(strID)) return PwgError.UnknownAlgorithm;
byte[] pbUuid = Convert.FromBase64String(strID); byte[] pbUuid = Convert.FromBase64String(strID);
PwUuid uuid = new PwUuid(pbUuid); PwUuid uuid = new PwUuid(pbUuid);
@@ -148,5 +135,57 @@ namespace KeePassLib.Cryptography.PasswordGenerator
psOut = pwd; psOut = pwd;
return PwgError.Success; return PwgError.Success;
} }
internal static string ErrorToString(PwgError e, bool bHeader)
{
if (e == PwgError.Success) { Debug.Assert(false); return string.Empty; }
if ((e == PwgError.Unknown) && bHeader) return KLRes.PwGenFailed;
string str = KLRes.UnknownError;
switch (e)
{
// case PwgError.Success:
// break;
case PwgError.Unknown:
break;
case PwgError.TooFewCharacters:
str = KLRes.CharSetTooFewChars;
break;
case PwgError.UnknownAlgorithm:
str = KLRes.AlgorithmUnknown;
break;
case PwgError.InvalidCharSet:
str = KLRes.CharSetInvalid;
break;
case PwgError.InvalidPattern:
str = KLRes.PatternInvalid;
break;
default:
Debug.Assert(false);
break;
}
if (bHeader)
str = KLRes.PwGenFailed + MessageService.NewParagraph + str;
return str;
}
internal static string ErrorToString(Exception ex, bool bHeader)
{
string str = ((ex == null) ? KLRes.UnknownError :
StrUtil.FormatException(ex));
if (bHeader)
str = KLRes.PwGenFailed + MessageService.NewParagraph + str;
return str;
}
} }
} }

View File

@@ -1,6 +1,6 @@
/* /*
KeePass Password Safe - The Open-Source Password Manager KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2017 Dominik Reichl <dominik.reichl@t-online.de> Copyright (C) 2003-2025 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@@ -19,8 +19,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text;
using System.Diagnostics; using System.Diagnostics;
using System.Text;
using KeePassLib.Utility; using KeePassLib.Utility;
@@ -28,17 +28,19 @@ namespace KeePassLib.Cryptography
{ {
public static class PopularPasswords public static class PopularPasswords
{ {
private static Dictionary<int, Dictionary<string, bool>> m_dicts = private static readonly Dictionary<int, Dictionary<char[], bool>> g_dicts =
new Dictionary<int, Dictionary<string, bool>>(); new Dictionary<int, Dictionary<char[], bool>>();
internal static int MaxLength internal static int MaxLength
{ {
get get
{ {
Debug.Assert(g_dicts.Count > 0); // Should be initialized
int iMaxLen = 0; int iMaxLen = 0;
foreach(int iLen in m_dicts.Keys) foreach (int iLen in g_dicts.Keys)
{ {
if(iLen > iMaxLen) iMaxLen = iLen; if (iLen > iMaxLen) iMaxLen = iLen;
} }
return iMaxLen; return iMaxLen;
@@ -47,8 +49,8 @@ namespace KeePassLib.Cryptography
internal static bool ContainsLength(int nLength) internal static bool ContainsLength(int nLength)
{ {
Dictionary<string, bool> dDummy; Dictionary<char[], bool> dDummy;
return m_dicts.TryGetValue(nLength, out dDummy); return g_dicts.TryGetValue(nLength, out dDummy);
} }
public static bool IsPopularPassword(char[] vPassword) public static bool IsPopularPassword(char[] vPassword)
@@ -59,74 +61,73 @@ namespace KeePassLib.Cryptography
public static bool IsPopularPassword(char[] vPassword, out ulong uDictSize) public static bool IsPopularPassword(char[] vPassword, out ulong uDictSize)
{ {
if(vPassword == null) throw new ArgumentNullException("vPassword"); if (vPassword == null) throw new ArgumentNullException("vPassword");
if(vPassword.Length == 0) { uDictSize = 0; return false; } if (vPassword.Length == 0) { uDictSize = 0; return false; }
string str = new string(vPassword); #if DEBUG
Array.ForEach(vPassword, ch => Debug.Assert(ch == char.ToLower(ch)));
#endif
try { return IsPopularPasswordPriv(str, out uDictSize); } try { return IsPopularPasswordPriv(vPassword, out uDictSize); }
catch(Exception) { Debug.Assert(false); } catch (Exception) { Debug.Assert(false); }
uDictSize = 0; uDictSize = 0;
return false; return false;
} }
private static bool IsPopularPasswordPriv(string str, out ulong uDictSize) private static bool IsPopularPasswordPriv(char[] vPassword, out ulong uDictSize)
{ {
Debug.Assert(m_dicts.Count > 0); // Should be initialized with data Debug.Assert(g_dicts.Count > 0); // Should be initialized with data
Dictionary<string, bool> d; Dictionary<char[], bool> d;
if(!m_dicts.TryGetValue(str.Length, out d)) if (!g_dicts.TryGetValue(vPassword.Length, out d))
{ {
uDictSize = 0; uDictSize = 0;
return false; return false;
} }
uDictSize = (ulong)d.Count; uDictSize = (ulong)d.Count;
return d.ContainsKey(str); return d.ContainsKey(vPassword);
} }
public static void Add(byte[] pbData, bool bGZipped) public static void Add(byte[] pbData, bool bGZipped)
{ {
try try
{ {
if(bGZipped) if (bGZipped)
pbData = MemUtil.Decompress(pbData); pbData = MemUtil.Decompress(pbData);
string strData = StrUtil.Utf8.GetString(pbData, 0, pbData.Length); string strData = StrUtil.Utf8.GetString(pbData, 0, pbData.Length);
if(string.IsNullOrEmpty(strData)) { Debug.Assert(false); return; } if (string.IsNullOrEmpty(strData)) { Debug.Assert(false); return; }
if(!char.IsWhiteSpace(strData[strData.Length - 1]))
strData += "\n";
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for(int i = 0; i < strData.Length; ++i) for (int i = 0; i <= strData.Length; ++i)
{ {
char ch = strData[i]; char ch = ((i == strData.Length) ? ' ' : strData[i]);
if(char.IsWhiteSpace(ch)) if (char.IsWhiteSpace(ch))
{ {
int cc = sb.Length; int cc = sb.Length;
if(cc > 0) if (cc > 0)
{ {
string strWord = sb.ToString(); char[] vWord = new char[cc];
Debug.Assert(strWord.Length == cc); sb.CopyTo(0, vWord, 0, cc);
Dictionary<string, bool> d; Dictionary<char[], bool> d;
if(!m_dicts.TryGetValue(cc, out d)) if (!g_dicts.TryGetValue(cc, out d))
{ {
d = new Dictionary<string, bool>(); d = new Dictionary<char[], bool>(MemUtil.ArrayHelperExOfChar);
m_dicts[cc] = d; g_dicts[cc] = d;
} }
d[strWord] = true; d[vWord] = true;
sb.Remove(0, cc); sb.Remove(0, cc);
} }
} }
else sb.Append(char.ToLower(ch)); else sb.Append(char.ToLower(ch));
} }
} }
catch(Exception) { Debug.Assert(false); } catch (Exception) { Debug.Assert(false); }
} }
} }
} }

View File

@@ -1,6 +1,6 @@
/* /*
KeePass Password Safe - The Open-Source Password Manager KeePass Password Safe - The Open-Source Password Manager
Copyright (C) 2003-2017 Dominik Reichl <dominik.reichl@t-online.de> Copyright (C) 2003-2025 Dominik Reichl <dominik.reichl@t-online.de>
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
@@ -19,8 +19,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text;
using System.Diagnostics; using System.Diagnostics;
using System.Text;
using KeePassLib.Cryptography.PasswordGenerator; using KeePassLib.Cryptography.PasswordGenerator;
using KeePassLib.Utility; using KeePassLib.Utility;
@@ -35,19 +35,19 @@ namespace KeePassLib.Cryptography
{ {
private static class PatternID private static class PatternID
{ {
public const char LowerAlpha = 'L'; internal const char LowerAlpha = 'L';
public const char UpperAlpha = 'U'; internal const char UpperAlpha = 'U';
public const char Digit = 'D'; internal const char Digit = 'D';
public const char Special = 'S'; internal const char Special = 'S';
public const char High = 'H'; internal const char Latin1S = 'H';
public const char Other = 'X'; internal const char Other = 'X';
public const char Dictionary = 'W'; internal const char Dictionary = 'W';
public const char Repetition = 'R'; internal const char Repetition = 'R';
public const char Number = 'N'; internal const char Number = 'N';
public const char DiffSeq = 'C'; internal const char DiffSeq = 'C';
public const string All = "LUDSHXWRNC"; internal const string All = "LUDSHXWRNC";
} }
// private static class CharDistrib // private static class CharDistrib
@@ -84,8 +84,8 @@ namespace KeePassLib.Cryptography
public QeCharType(char chTypeID, string strAlphabet, bool bIsConsecutive) public QeCharType(char chTypeID, string strAlphabet, bool bIsConsecutive)
{ {
if(strAlphabet == null) throw new ArgumentNullException(); if (strAlphabet == null) throw new ArgumentNullException();
if(strAlphabet.Length == 0) throw new ArgumentException(); if (strAlphabet.Length == 0) throw new ArgumentException();
m_chTypeID = chTypeID; m_chTypeID = chTypeID;
m_strAlph = strAlphabet; m_strAlph = strAlphabet;
@@ -101,7 +101,7 @@ namespace KeePassLib.Cryptography
public QeCharType(char chTypeID, int nChars) // Catch-none set public QeCharType(char chTypeID, int nChars) // Catch-none set
{ {
if(nChars <= 0) throw new ArgumentOutOfRangeException(); if (nChars <= 0) throw new ArgumentOutOfRangeException();
m_chTypeID = chTypeID; m_chTypeID = chTypeID;
m_strAlph = string.Empty; m_strAlph = string.Empty;
@@ -114,7 +114,7 @@ namespace KeePassLib.Cryptography
public bool Contains(char ch) public bool Contains(char ch)
{ {
if(m_chLast != char.MinValue) if (m_chLast != char.MinValue)
return ((ch >= m_chFirst) && (ch <= m_chLast)); return ((ch >= m_chFirst) && (ch <= m_chLast));
Debug.Assert(m_strAlph.Length > 0); // Don't call for catch-none set Debug.Assert(m_strAlph.Length > 0); // Don't call for catch-none set
@@ -125,7 +125,7 @@ namespace KeePassLib.Cryptography
private sealed class EntropyEncoder private sealed class EntropyEncoder
{ {
private readonly string m_strAlph; private readonly string m_strAlph;
private Dictionary<char, ulong> m_dHisto = new Dictionary<char, ulong>(); private readonly Dictionary<char, ulong> m_dHisto = new Dictionary<char, ulong>();
private readonly ulong m_uBaseWeight; private readonly ulong m_uBaseWeight;
private readonly ulong m_uCharWeight; private readonly ulong m_uCharWeight;
private readonly ulong m_uOccExclThreshold; private readonly ulong m_uOccExclThreshold;
@@ -133,8 +133,8 @@ namespace KeePassLib.Cryptography
public EntropyEncoder(string strAlphabet, ulong uBaseWeight, public EntropyEncoder(string strAlphabet, ulong uBaseWeight,
ulong uCharWeight, ulong uOccExclThreshold) ulong uCharWeight, ulong uOccExclThreshold)
{ {
if(strAlphabet == null) throw new ArgumentNullException(); if (strAlphabet == null) throw new ArgumentNullException();
if(strAlphabet.Length == 0) throw new ArgumentException(); if (strAlphabet.Length == 0) throw new ArgumentException();
m_strAlph = strAlphabet; m_strAlph = strAlphabet;
m_uBaseWeight = uBaseWeight; m_uBaseWeight = uBaseWeight;
@@ -143,7 +143,7 @@ namespace KeePassLib.Cryptography
#if DEBUG #if DEBUG
Dictionary<char, bool> d = new Dictionary<char, bool>(); Dictionary<char, bool> d = new Dictionary<char, bool>();
foreach(char ch in m_strAlph) { d[ch] = true; } foreach (char ch in m_strAlph) { d[ch] = true; }
Debug.Assert(d.Count == m_strAlph.Length); // No duplicates Debug.Assert(d.Count == m_strAlph.Length); // No duplicates
#endif #endif
} }
@@ -166,18 +166,18 @@ namespace KeePassLib.Cryptography
public double GetOutputSize() public double GetOutputSize()
{ {
ulong uTotalWeight = m_uBaseWeight * (ulong)m_strAlph.Length; ulong uTotalWeight = m_uBaseWeight * (ulong)m_strAlph.Length;
foreach(ulong u in m_dHisto.Values) foreach (ulong u in m_dHisto.Values)
{ {
Debug.Assert(u >= 1); Debug.Assert(u >= 1);
if(u > m_uOccExclThreshold) if (u > m_uOccExclThreshold)
uTotalWeight += (u - m_uOccExclThreshold) * m_uCharWeight; uTotalWeight += (u - m_uOccExclThreshold) * m_uCharWeight;
} }
double dSize = 0.0, dTotalWeight = (double)uTotalWeight; double dSize = 0.0, dTotalWeight = (double)uTotalWeight;
foreach(ulong u in m_dHisto.Values) foreach (ulong u in m_dHisto.Values)
{ {
ulong uWeight = m_uBaseWeight; ulong uWeight = m_uBaseWeight;
if(u > m_uOccExclThreshold) if (u > m_uOccExclThreshold)
uWeight += (u - m_uOccExclThreshold) * m_uCharWeight; uWeight += (u - m_uOccExclThreshold) * m_uCharWeight;
dSize -= (double)u * Log2((double)uWeight / dTotalWeight); dSize -= (double)u * Log2((double)uWeight / dTotalWeight);
@@ -189,7 +189,7 @@ namespace KeePassLib.Cryptography
private sealed class MultiEntropyEncoder private sealed class MultiEntropyEncoder
{ {
private Dictionary<char, EntropyEncoder> m_dEncs = private readonly Dictionary<char, EntropyEncoder> m_dEncs =
new Dictionary<char, EntropyEncoder>(); new Dictionary<char, EntropyEncoder>();
public MultiEntropyEncoder() public MultiEntropyEncoder()
@@ -198,7 +198,7 @@ namespace KeePassLib.Cryptography
public void AddEncoder(char chTypeID, EntropyEncoder ec) public void AddEncoder(char chTypeID, EntropyEncoder ec)
{ {
if(ec == null) { Debug.Assert(false); return; } if (ec == null) { Debug.Assert(false); return; }
Debug.Assert(!m_dEncs.ContainsKey(chTypeID)); Debug.Assert(!m_dEncs.ContainsKey(chTypeID));
m_dEncs[chTypeID] = ec; m_dEncs[chTypeID] = ec;
@@ -206,13 +206,13 @@ namespace KeePassLib.Cryptography
public void Reset() public void Reset()
{ {
foreach(EntropyEncoder ec in m_dEncs.Values) { ec.Reset(); } foreach (EntropyEncoder ec in m_dEncs.Values) { ec.Reset(); }
} }
public bool Write(char chTypeID, char chData) public bool Write(char chTypeID, char chData)
{ {
EntropyEncoder ec; EntropyEncoder ec;
if(!m_dEncs.TryGetValue(chTypeID, out ec)) if (!m_dEncs.TryGetValue(chTypeID, out ec))
return false; return false;
ec.Write(chData); ec.Write(chData);
@@ -223,7 +223,7 @@ namespace KeePassLib.Cryptography
{ {
double d = 0.0; double d = 0.0;
foreach(EntropyEncoder ec in m_dEncs.Values) foreach (EntropyEncoder ec in m_dEncs.Values)
{ {
d += ec.GetOutputSize(); d += ec.GetOutputSize();
} }
@@ -281,36 +281,31 @@ namespace KeePassLib.Cryptography
} }
} }
private static object m_objSyncInit = new object(); private static readonly object m_objSyncInit = new object();
private static List<QeCharType> m_lCharTypes = null; private static List<QeCharType> m_lCharTypes = null;
private static void EnsureInitialized() private static void EnsureInitialized()
{ {
lock(m_objSyncInit) lock (m_objSyncInit)
{ {
if(m_lCharTypes == null) if (m_lCharTypes == null)
{ {
string strSpecial = PwCharSet.PrintableAsciiSpecial; string strSpecial = PwCharSet.PrintableAsciiSpecial;
if(strSpecial.IndexOf(' ') >= 0) { Debug.Assert(false); } if (strSpecial.IndexOf(' ') >= 0) { Debug.Assert(false); }
else strSpecial = strSpecial + " "; else strSpecial += " ";
int nSp = strSpecial.Length; int nSp = strSpecial.Length;
int nHi = PwCharSet.HighAnsiChars.Length; int nL1S = PwCharSet.Latin1S.Length;
m_lCharTypes = new List<QeCharType>(); m_lCharTypes = new List<QeCharType>()
{
m_lCharTypes.Add(new QeCharType(PatternID.LowerAlpha, new QeCharType(PatternID.LowerAlpha, PwCharSet.LowerCase, true),
PwCharSet.LowerCase, true)); new QeCharType(PatternID.UpperAlpha, PwCharSet.UpperCase, true),
m_lCharTypes.Add(new QeCharType(PatternID.UpperAlpha, new QeCharType(PatternID.Digit, PwCharSet.Digits, true),
PwCharSet.UpperCase, true)); new QeCharType(PatternID.Special, strSpecial, false),
m_lCharTypes.Add(new QeCharType(PatternID.Digit, new QeCharType(PatternID.Latin1S, PwCharSet.Latin1S, false),
PwCharSet.Digits, true)); new QeCharType(PatternID.Other, 0x10000 - (2 * 26) - 10 - nSp - nL1S)
m_lCharTypes.Add(new QeCharType(PatternID.Special, };
strSpecial, false));
m_lCharTypes.Add(new QeCharType(PatternID.High,
PwCharSet.HighAnsiChars, false));
m_lCharTypes.Add(new QeCharType(PatternID.Other,
0x10000 - (2 * 26) - 10 - nSp - nHi));
} }
} }
} }
@@ -318,37 +313,37 @@ namespace KeePassLib.Cryptography
/// <summary> /// <summary>
/// Estimate the quality of a password. /// Estimate the quality of a password.
/// </summary> /// </summary>
/// <param name="vPasswordChars">Password to check.</param> /// <param name="vPassword">Password to check.</param>
/// <returns>Estimated bit-strength of the password.</returns> /// <returns>Estimated bit-strength of the password.</returns>
public static uint EstimatePasswordBits(char[] vPasswordChars) public static uint EstimatePasswordBits(char[] vPassword)
{ {
if(vPasswordChars == null) { Debug.Assert(false); return 0; } if (vPassword == null) { Debug.Assert(false); return 0; }
if(vPasswordChars.Length == 0) return 0; if (vPassword.Length == 0) return 0;
EnsureInitialized(); EnsureInitialized();
int n = vPasswordChars.Length; int n = vPassword.Length;
List<QePatternInstance>[] vPatterns = new List<QePatternInstance>[n]; List<QePatternInstance>[] vPatterns = new List<QePatternInstance>[n];
for(int i = 0; i < n; ++i) for (int i = 0; i < n; ++i)
{ {
vPatterns[i] = new List<QePatternInstance>(); vPatterns[i] = new List<QePatternInstance>();
QePatternInstance piChar = new QePatternInstance(i, 1, QePatternInstance piChar = new QePatternInstance(i, 1,
GetCharType(vPasswordChars[i])); GetCharType(vPassword[i]));
vPatterns[i].Add(piChar); vPatterns[i].Add(piChar);
} }
FindRepetitions(vPasswordChars, vPatterns); FindRepetitions(vPassword, vPatterns);
FindNumbers(vPasswordChars, vPatterns); FindNumbers(vPassword, vPatterns);
FindDiffSeqs(vPasswordChars, vPatterns); FindDiffSeqs(vPassword, vPatterns);
FindPopularPasswords(vPasswordChars, vPatterns); FindPopularPasswords(vPassword, vPatterns);
// Encoders must not be static, because the entropy estimation // Encoders must not be static, because the entropy estimation
// may run concurrently in multiple threads and the encoders are // may run concurrently in multiple threads and the encoders are
// not read-only // not read-only
EntropyEncoder ecPattern = new EntropyEncoder(PatternID.All, 0, 1, 0); EntropyEncoder ecPattern = new EntropyEncoder(PatternID.All, 0, 1, 0);
MultiEntropyEncoder mcData = new MultiEntropyEncoder(); MultiEntropyEncoder mcData = new MultiEntropyEncoder();
for(int i = 0; i < (m_lCharTypes.Count - 1); ++i) for (int i = 0; i < (m_lCharTypes.Count - 1); ++i)
{ {
// Let m be the alphabet size. In order to ensure that two same // Let m be the alphabet size. In order to ensure that two same
// characters cost at least as much as a single character, for // characters cost at least as much as a single character, for
@@ -371,25 +366,25 @@ namespace KeePassLib.Cryptography
Stack<QePathState> sRec = new Stack<QePathState>(); Stack<QePathState> sRec = new Stack<QePathState>();
sRec.Push(new QePathState(0, new List<QePatternInstance>())); sRec.Push(new QePathState(0, new List<QePatternInstance>()));
while(sRec.Count > 0) while (sRec.Count > 0)
{ {
int tDiff = Environment.TickCount - tStart; int tDiff = Environment.TickCount - tStart;
if(tDiff > 500) break; if (tDiff > 500) break;
QePathState s = sRec.Pop(); QePathState s = sRec.Pop();
if(s.Position >= n) if (s.Position >= n)
{ {
Debug.Assert(s.Position == n); Debug.Assert(s.Position == n);
double dblCost = ComputePathCost(s.Path, vPasswordChars, double dblCost = ComputePathCost(s.Path, vPassword,
ecPattern, mcData); ecPattern, mcData);
if(dblCost < dblMinCost) dblMinCost = dblCost; if (dblCost < dblMinCost) dblMinCost = dblCost;
} }
else else
{ {
List<QePatternInstance> lSubs = vPatterns[s.Position]; List<QePatternInstance> lSubs = vPatterns[s.Position];
for(int i = lSubs.Count - 1; i >= 0; --i) for (int i = lSubs.Count - 1; i >= 0; --i)
{ {
QePatternInstance pi = lSubs[i]; QePatternInstance pi = lSubs[i];
Debug.Assert(pi.Position == s.Position); Debug.Assert(pi.Position == s.Position);
@@ -418,13 +413,14 @@ namespace KeePassLib.Cryptography
/// <returns>Estimated bit-strength of the password.</returns> /// <returns>Estimated bit-strength of the password.</returns>
public static uint EstimatePasswordBits(byte[] pbUnprotectedUtf8) public static uint EstimatePasswordBits(byte[] pbUnprotectedUtf8)
{ {
if(pbUnprotectedUtf8 == null) { Debug.Assert(false); return 0; } if (pbUnprotectedUtf8 == null) { Debug.Assert(false); return 0; }
char[] vChars = StrUtil.Utf8.GetChars(pbUnprotectedUtf8); char[] v = StrUtil.Utf8.GetChars(pbUnprotectedUtf8);
uint uResult = EstimatePasswordBits(vChars); uint r;
MemUtil.ZeroArray<char>(vChars); try { r = EstimatePasswordBits(v); }
finally { MemUtil.ZeroArray<char>(v); }
return uResult; return r;
} }
private static QeCharType GetCharType(char ch) private static QeCharType GetCharType(char ch)
@@ -432,9 +428,9 @@ namespace KeePassLib.Cryptography
int nTypes = m_lCharTypes.Count; int nTypes = m_lCharTypes.Count;
Debug.Assert((nTypes > 0) && (m_lCharTypes[nTypes - 1].CharCount > 256)); Debug.Assert((nTypes > 0) && (m_lCharTypes[nTypes - 1].CharCount > 256));
for(int i = 0; i < (nTypes - 1); ++i) for (int i = 0; i < (nTypes - 1); ++i)
{ {
if(m_lCharTypes[i].Contains(ch)) if (m_lCharTypes[i].Contains(ch))
return m_lCharTypes[i]; return m_lCharTypes[i];
} }
@@ -445,19 +441,19 @@ namespace KeePassLib.Cryptography
char[] vPassword, EntropyEncoder ecPattern, MultiEntropyEncoder mcData) char[] vPassword, EntropyEncoder ecPattern, MultiEntropyEncoder mcData)
{ {
ecPattern.Reset(); ecPattern.Reset();
for(int i = 0; i < l.Count; ++i) for (int i = 0; i < l.Count; ++i)
ecPattern.Write(l[i].PatternID); ecPattern.Write(l[i].PatternID);
double dblPatternCost = ecPattern.GetOutputSize(); double dblPatternCost = ecPattern.GetOutputSize();
mcData.Reset(); mcData.Reset();
double dblDataCost = 0.0; double dblDataCost = 0.0;
foreach(QePatternInstance pi in l) foreach (QePatternInstance pi in l)
{ {
QeCharType tChar = pi.SingleCharType; QeCharType tChar = pi.SingleCharType;
if(tChar != null) if (tChar != null)
{ {
char ch = vPassword[pi.Position]; char ch = vPassword[pi.Position];
if(!mcData.Write(tChar.TypeID, ch)) if (!mcData.Write(tChar.TypeID, ch))
dblDataCost += pi.Cost; dblDataCost += pi.Cost;
} }
else dblDataCost += pi.Cost; else dblDataCost += pi.Cost;
@@ -474,7 +470,7 @@ namespace KeePassLib.Cryptography
char[] vLower = new char[n]; char[] vLower = new char[n];
char[] vLeet = new char[n]; char[] vLeet = new char[n];
for(int i = 0; i < n; ++i) for (int i = 0; i < n; ++i)
{ {
char ch = vPassword[i]; char ch = vPassword[i];
@@ -482,27 +478,27 @@ namespace KeePassLib.Cryptography
vLeet[i] = char.ToLower(DecodeLeetChar(ch)); vLeet[i] = char.ToLower(DecodeLeetChar(ch));
} }
char chErased = default(char); char chErased = default(char); // The value that Array.Clear uses
Debug.Assert(chErased == char.MinValue); Debug.Assert(chErased == char.MinValue);
int nMaxLen = Math.Min(n, PopularPasswords.MaxLength); int nMaxLen = Math.Min(n, PopularPasswords.MaxLength);
for(int nSubLen = nMaxLen; nSubLen >= 3; --nSubLen) for (int nSubLen = nMaxLen; nSubLen >= 3; --nSubLen)
{ {
if(!PopularPasswords.ContainsLength(nSubLen)) continue; if (!PopularPasswords.ContainsLength(nSubLen)) continue;
char[] vSub = new char[nSubLen]; char[] vSub = new char[nSubLen];
for(int i = 0; i <= (n - nSubLen); ++i) for (int i = 0; i <= (n - nSubLen); ++i)
{ {
if(Array.IndexOf<char>(vLower, chErased, i, nSubLen) >= 0) if (Array.IndexOf<char>(vLower, chErased, i, nSubLen) >= 0)
continue; continue;
Array.Copy(vLower, i, vSub, 0, nSubLen); Array.Copy(vLower, i, vSub, 0, nSubLen);
if(!EvalAddPopularPasswordPattern(vPatterns, vPassword, if (!EvalAddPopularPasswordPattern(vPatterns, vPassword,
i, vSub, 0.0)) i, vSub, 0.0))
{ {
Array.Copy(vLeet, i, vSub, 0, nSubLen); Array.Copy(vLeet, i, vSub, 0, nSubLen);
if(EvalAddPopularPasswordPattern(vPatterns, vPassword, if (EvalAddPopularPasswordPattern(vPatterns, vPassword,
i, vSub, 1.5)) i, vSub, 1.5))
{ {
Array.Clear(vLower, i, nSubLen); // Not vLeet Array.Clear(vLower, i, nSubLen); // Not vLeet
@@ -515,14 +511,19 @@ namespace KeePassLib.Cryptography
Debug.Assert(vLower[i] == chErased); Debug.Assert(vLower[i] == chErased);
} }
} }
MemUtil.ZeroArray<char>(vSub);
} }
MemUtil.ZeroArray<char>(vLower);
MemUtil.ZeroArray<char>(vLeet);
} }
private static bool EvalAddPopularPasswordPattern(List<QePatternInstance>[] vPatterns, private static bool EvalAddPopularPasswordPattern(List<QePatternInstance>[] vPatterns,
char[] vPassword, int i, char[] vSub, double dblCostPerMod) char[] vPassword, int i, char[] vSub, double dblCostPerMod)
{ {
ulong uDictSize; ulong uDictSize;
if(!PopularPasswords.IsPopularPassword(vSub, out uDictSize)) if (!PopularPasswords.IsPopularPassword(vSub, out uDictSize))
return false; return false;
int n = vSub.Length; int n = vSub.Length;
@@ -532,9 +533,9 @@ namespace KeePassLib.Cryptography
// dblCost += log2(n binom d) // dblCost += log2(n binom d)
int k = Math.Min(d, n - d); int k = Math.Min(d, n - d);
for(int j = n; j > (n - k); --j) for (int j = n; j > (n - k); --j)
dblCost += Log2(j); dblCost += Log2(j);
for(int j = k; j >= 2; --j) for (int j = k; j >= 2; --j)
dblCost -= Log2(j); dblCost -= Log2(j);
dblCost += dblCostPerMod * (double)d; dblCost += dblCostPerMod * (double)d;
@@ -546,19 +547,19 @@ namespace KeePassLib.Cryptography
private static char DecodeLeetChar(char chLeet) private static char DecodeLeetChar(char chLeet)
{ {
if((chLeet >= '\u00C0') && (chLeet <= '\u00C6')) return 'a'; if ((chLeet >= '\u00C0') && (chLeet <= '\u00C6')) return 'a';
if((chLeet >= '\u00C8') && (chLeet <= '\u00CB')) return 'e'; if ((chLeet >= '\u00C8') && (chLeet <= '\u00CB')) return 'e';
if((chLeet >= '\u00CC') && (chLeet <= '\u00CF')) return 'i'; if ((chLeet >= '\u00CC') && (chLeet <= '\u00CF')) return 'i';
if((chLeet >= '\u00D2') && (chLeet <= '\u00D6')) return 'o'; if ((chLeet >= '\u00D2') && (chLeet <= '\u00D6')) return 'o';
if((chLeet >= '\u00D9') && (chLeet <= '\u00DC')) return 'u'; if ((chLeet >= '\u00D9') && (chLeet <= '\u00DC')) return 'u';
if((chLeet >= '\u00E0') && (chLeet <= '\u00E6')) return 'a'; if ((chLeet >= '\u00E0') && (chLeet <= '\u00E6')) return 'a';
if((chLeet >= '\u00E8') && (chLeet <= '\u00EB')) return 'e'; if ((chLeet >= '\u00E8') && (chLeet <= '\u00EB')) return 'e';
if((chLeet >= '\u00EC') && (chLeet <= '\u00EF')) return 'i'; if ((chLeet >= '\u00EC') && (chLeet <= '\u00EF')) return 'i';
if((chLeet >= '\u00F2') && (chLeet <= '\u00F6')) return 'o'; if ((chLeet >= '\u00F2') && (chLeet <= '\u00F6')) return 'o';
if((chLeet >= '\u00F9') && (chLeet <= '\u00FC')) return 'u'; if ((chLeet >= '\u00F9') && (chLeet <= '\u00FC')) return 'u';
char ch; char ch;
switch(chLeet) switch (chLeet)
{ {
case '4': case '4':
case '@': case '@':
@@ -621,9 +622,9 @@ namespace KeePassLib.Cryptography
char[] v2, int iOffset2, int nLength) char[] v2, int iOffset2, int nLength)
{ {
int nDist = 0; int nDist = 0;
for(int i = 0; i < nLength; ++i) for (int i = 0; i < nLength; ++i)
{ {
if(v1[iOffset1 + i] != v2[iOffset2 + i]) ++nDist; if (v1[iOffset1 + i] != v2[iOffset2 + i]) ++nDist;
} }
return nDist; return nDist;
@@ -637,15 +638,15 @@ namespace KeePassLib.Cryptography
Array.Copy(vPassword, v, n); Array.Copy(vPassword, v, n);
char chErased = char.MaxValue; char chErased = char.MaxValue;
for(int m = (n / 2); m >= 3; --m) for (int m = (n / 2); m >= 3; --m)
{ {
for(int x1 = 0; x1 <= (n - (2 * m)); ++x1) for (int x1 = 0; x1 <= (n - (2 * m)); ++x1)
{ {
bool bFoundRep = false; bool bFoundRep = false;
for(int x2 = (x1 + m); x2 <= (n - m); ++x2) for (int x2 = (x1 + m); x2 <= (n - m); ++x2)
{ {
if(PartsEqual(v, x1, x2, m)) if (PartsEqual(v, x1, x2, m))
{ {
double dblCost = Log2(x1 + 1) + Log2(m); double dblCost = Log2(x1 + 1) + Log2(m);
vPatterns[x2].Add(new QePatternInstance(x2, m, vPatterns[x2].Add(new QePatternInstance(x2, m,
@@ -656,16 +657,18 @@ namespace KeePassLib.Cryptography
} }
} }
if(bFoundRep) ErasePart(v, x1, m, ref chErased); if (bFoundRep) ErasePart(v, x1, m, ref chErased);
} }
} }
MemUtil.ZeroArray<char>(v);
} }
private static bool PartsEqual(char[] v, int x1, int x2, int nLength) private static bool PartsEqual(char[] v, int x1, int x2, int nLength)
{ {
for(int i = 0; i < nLength; ++i) for (int i = 0; i < nLength; ++i)
{ {
if(v[x1 + i] != v[x2 + i]) return false; if (v[x1 + i] != v[x2 + i]) return false;
} }
return true; return true;
@@ -673,7 +676,7 @@ namespace KeePassLib.Cryptography
private static void ErasePart(char[] v, int i, int n, ref char chErased) private static void ErasePart(char[] v, int i, int n, ref char chErased)
{ {
for(int j = 0; j < n; ++j) for (int j = 0; j < n; ++j)
{ {
v[i + j] = chErased; v[i + j] = chErased;
--chErased; --chErased;
@@ -685,33 +688,35 @@ namespace KeePassLib.Cryptography
{ {
int n = vPassword.Length; int n = vPassword.Length;
StringBuilder sb = new StringBuilder(); StringBuilder sb = new StringBuilder();
for(int i = 0; i < n; ++i)
for (int i = 0; i < n; ++i)
{ {
char ch = vPassword[i]; char ch = vPassword[i];
if((ch >= '0') && (ch <= '9')) sb.Append(ch); if ((ch >= '0') && (ch <= '9')) sb.Append(ch);
else else
{ {
AddNumberPattern(vPatterns, sb.ToString(), i - sb.Length); AddNumberPattern(vPatterns, sb, i - sb.Length);
sb.Remove(0, sb.Length); sb.Remove(0, sb.Length);
} }
} }
AddNumberPattern(vPatterns, sb.ToString(), n - sb.Length); AddNumberPattern(vPatterns, sb, n - sb.Length);
} }
private static void AddNumberPattern(List<QePatternInstance>[] vPatterns, private static void AddNumberPattern(List<QePatternInstance>[] vPatterns,
string strNumber, int i) StringBuilder sb, int i)
{ {
if(strNumber.Length <= 2) return; if (sb.Length <= 2) return;
string strNumber = sb.ToString();
int nZeros = 0; int nZeros = 0;
for(int j = 0; j < strNumber.Length; ++j) for (int j = 0; j < strNumber.Length; ++j)
{ {
if(strNumber[j] != '0') break; if (strNumber[j] != '0') break;
++nZeros; ++nZeros;
} }
double dblCost = Log2(nZeros + 1); double dblCost = Log2(nZeros + 1);
if(nZeros < strNumber.Length) if (nZeros < strNumber.Length)
{ {
string strNonZero = strNumber.Substring(nZeros); string strNonZero = strNumber.Substring(nZeros);
@@ -720,7 +725,7 @@ namespace KeePassLib.Cryptography
catch(Exception) { Debug.Assert(false); return; } catch(Exception) { Debug.Assert(false); return; }
#else #else
double d; double d;
if(double.TryParse(strNonZero, out d)) if (double.TryParse(strNonZero, out d))
dblCost += Log2(d); dblCost += Log2(d);
else { Debug.Assert(false); return; } else { Debug.Assert(false); return; }
#endif #endif
@@ -733,17 +738,18 @@ namespace KeePassLib.Cryptography
private static void FindDiffSeqs(char[] vPassword, private static void FindDiffSeqs(char[] vPassword,
List<QePatternInstance>[] vPatterns) List<QePatternInstance>[] vPatterns)
{ {
int d = int.MinValue, p = 0; int n = vPassword.Length;
string str = new string(vPassword) + new string(char.MaxValue, 1); int d = int.MaxValue, p = 0;
for(int i = 1; i < str.Length; ++i) for (int i = 1; i <= n; ++i)
{ {
int dCur = (int)str[i] - (int)str[i - 1]; int dCur = ((i == n) ? int.MinValue :
if(dCur != d) ((int)vPassword[i] - (int)vPassword[i - 1]));
if (dCur != d)
{ {
if((i - p) >= 3) // At least 3 chars involved if ((i - p) >= 3) // At least 3 chars involved
{ {
QeCharType ct = GetCharType(str[p]); QeCharType ct = GetCharType(vPassword[p]);
double dblCost = ct.CharSize + Log2(i - p - 1); double dblCost = ct.CharSize + Log2(i - p - 1);
vPatterns[p].Add(new QePatternInstance(p, vPatterns[p].Add(new QePatternInstance(p,

View File

@@ -46,4 +46,12 @@ namespace KeePassLib.Delegates
public delegate void VoidDelegate(); public delegate void VoidDelegate();
public delegate string StrPwEntryDelegate(string str, PwEntry pe); public delegate string StrPwEntryDelegate(string str, PwEntry pe);
public delegate TResult GFunc<TResult>();
public delegate TResult GFunc<T, TResult>(T o);
public delegate TResult GFunc<T1, T2, TResult>(T1 o1, T2 o2);
public delegate TResult GFunc<T1, T2, T3, TResult>(T1 o1, T2 o2, T3 o3);
public delegate TResult GFunc<T1, T2, T3, T4, TResult>(T1 o1, T2 o2, T3 o3, T4 o4);
public delegate TResult GFunc<T1, T2, T3, T4, T5, TResult>(T1 o1, T2 o2, T3 o3, T4 o4, T5 o5);
public delegate TResult GFunc<T1, T2, T3, T4, T5, T6, TResult>(T1 o1, T2 o2, T3 o3, T4 o4, T5 o5, T6 o6);
} }

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

View File

@@ -1546,10 +1546,10 @@ namespace keepass2android
string url = _stringViews[urlFieldKey].Text; string url = _stringViews[urlFieldKey].Text;
if (url == null) return false; if (url == null) return false;
// Default http:// if no protocol specified // Default https:// if no protocol specified
if ((!url.Contains(":") || (url.StartsWith("www.")))) if ((!url.Contains(":") || (url.StartsWith("www."))))
{ {
url = "http://" + url; url = "https://" + url;
} }
try try

View File

@@ -23,6 +23,7 @@ using Android.App;
using Android.App.Admin; using Android.App.Admin;
using Android.Content; using Android.Content;
using Android.Content.PM; using Android.Content.PM;
using Android.Content.Res;
using Android.Graphics; using Android.Graphics;
using Android.OS; using Android.OS;
using Android.Preferences; using Android.Preferences;
@@ -66,8 +67,13 @@ namespace keepass2android
Resource.Id.cb_exclude_lookalike Resource.Id.cb_exclude_lookalike
}; };
PasswordFont _passwordFont = new PasswordFont(); PasswordFont _passwordFont = new PasswordFont();
private static object _popularPasswordsLock = new object();
private static bool _popularPasswordsInitialized = false;
private ActivityDesign _design; private ActivityDesign _design;
public GeneratePasswordActivity() public GeneratePasswordActivity()
@@ -302,6 +308,10 @@ namespace keepass2android
EditText txtPasswordToSet = (EditText) FindViewById(Resource.Id.password_edit); EditText txtPasswordToSet = (EditText) FindViewById(Resource.Id.password_edit);
txtPasswordToSet.TextChanged += (sender, args) =>
{
Task.Run(() => UpdatePasswordStrengthEstimate(txtPasswordToSet.Text));
};
_passwordFont.ApplyTo(txtPasswordToSet); _passwordFont.ApplyTo(txtPasswordToSet);
@@ -467,17 +477,48 @@ namespace keepass2android
return; return;
String password = ""; String password = "";
uint passwordBits = 0;
Task.Run(() => Task.Run(() =>
{ {
password = GeneratePassword(); password = GeneratePassword();
passwordBits = QualityEstimation.EstimatePasswordBits(password.ToCharArray());
RunOnUiThread(() => RunOnUiThread(() =>
{ {
EditText txtPassword = (EditText)FindViewById(Resource.Id.password_edit); EditText txtPassword = (EditText)FindViewById(Resource.Id.password_edit);
txtPassword.Text = password; txtPassword.Text = password;
UpdateProfileSpinnerSelection();
});
});
}
private void UpdatePasswordStrengthEstimate(string password)
{
lock (_popularPasswordsLock)
{
if (!_popularPasswordsInitialized)
{
using (StreamReader sr = new StreamReader(Assets.Open("MostPopularPasswords.txt")))
{
var bytes = default(byte[]);
using (var memstream = new MemoryStream())
{
sr.BaseStream.CopyTo(memstream);
bytes = memstream.ToArray();
}
PopularPasswords.Add(bytes, false);
}
}
}
uint passwordBits = QualityEstimation.EstimatePasswordBits(password.ToCharArray());
RunOnUiThread(() =>
{
var progressBar = FindViewById<ProgressBar>(Resource.Id.pb_password_strength); var progressBar = FindViewById<ProgressBar>(Resource.Id.pb_password_strength);
progressBar.Progress = (int)passwordBits; progressBar.Progress = (int)passwordBits;
@@ -503,13 +544,7 @@ namespace keepass2android
PorterDuff.Mode.SrcIn)); PorterDuff.Mode.SrcIn));
FindViewById<TextView>(Resource.Id.tv_password_strength).Text = " " + passwordBits + " bits"; FindViewById<TextView>(Resource.Id.tv_password_strength).Text = " " + passwordBits + " bits";
UpdateProfileSpinnerSelection();
}); });
});
} }
private void UpdateProfileSpinnerSelection() private void UpdateProfileSpinnerSelection()

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="215" android:versionCode="218"
android:versionName="1.12-r9" android:versionName="1.12-r9d"
package="keepass2android.keepass2android" package="keepass2android.keepass2android"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:installLocation="auto"> android:installLocation="auto">

View File

@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" <manifest xmlns:android="http://schemas.android.com/apk/res/android"
android:versionCode="215" android:versionCode="218"
android:versionName="1.12-r9" android:versionName="1.12-r9d"
package="keepass2android.keepass2android_nonet" package="keepass2android.keepass2android_nonet"
xmlns:tools="http://schemas.android.com/tools" xmlns:tools="http://schemas.android.com/tools"
android:installLocation="auto"> android:installLocation="auto">

View File

@@ -1423,6 +1423,8 @@ namespace keepass2android
if (cbQuickUnlock == null) if (cbQuickUnlock == null)
throw new NullPointerException("cpQuickUnlock"); throw new NullPointerException("cpQuickUnlock");
App.Kp2a.SetQuickUnlockEnabled(cbQuickUnlock.Checked); App.Kp2a.SetQuickUnlockEnabled(cbQuickUnlock.Checked);
App.Kp2a.ScreenLockWasEnabledWhenOpeningDatabase =
(((KeyguardManager)GetSystemService(Context.KeyguardService)!)!).IsDeviceSecure;
if ((_loadDbFileTask != null) && (App.Kp2a.OfflineMode != _loadDbTaskOffline)) if ((_loadDbFileTask != null) && (App.Kp2a.OfflineMode != _loadDbTaskOffline))
{ {

View File

@@ -25,6 +25,7 @@ using Android.Widget;
using Android.Content.PM; using Android.Content.PM;
using KeePassLib.Keys; using KeePassLib.Keys;
using Android.Preferences; using Android.Preferences;
using Android.Provider;
using Android.Runtime; using Android.Runtime;
using Android.Views.InputMethods; using Android.Views.InputMethods;
@@ -162,6 +163,29 @@ namespace keepass2android
if (bundle != null) if (bundle != null)
numFailedAttempts = bundle.GetInt(NumFailedAttemptsKey, 0); numFailedAttempts = bundle.GetInt(NumFailedAttemptsKey, 0);
FindViewById(Resource.Id.QuickUnlock_buttonEnableLock).Click += (object sender, EventArgs e) =>
{
Intent intent = new Intent(Settings.ActionSecuritySettings);
StartActivity(intent);
};
FindViewById(Resource.Id.QuickUnlock_buttonCloseDb).Click += (object sender, EventArgs e) =>
{
App.Kp2a.Lock(false);
};
if (App.Kp2a.ScreenLockWasEnabledWhenOpeningDatabase == false)
{
FindViewById(Resource.Id.QuickUnlockForm).Visibility = ViewStates.Gone;
FindViewById(Resource.Id.QuickUnlockBlocked).Visibility = ViewStates.Visible;
}
else
{
FindViewById(Resource.Id.QuickUnlockForm).Visibility = ViewStates.Visible;
FindViewById(Resource.Id.QuickUnlockBlocked).Visibility = ViewStates.Gone;
}
} }

View File

@@ -79,6 +79,12 @@ android:paddingRight="16dp"
android:paddingTop="16dp"> android:paddingTop="16dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="@+id/QuickUnlockForm">
<TextView <TextView
android:id="@+id/QuickUnlock_label" android:id="@+id/QuickUnlock_label"
android:text="@string/QuickUnlock_label" android:text="@string/QuickUnlock_label"
@@ -88,11 +94,6 @@ android:paddingRight="16dp"
android:textSize="14sp" android:textSize="14sp"
/> />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText <EditText
android:inputType="textPassword" android:inputType="textPassword"
android:layout_width="wrap_content" android:layout_width="wrap_content"
@@ -121,6 +122,60 @@ android:paddingRight="16dp"
</LinearLayout> </LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@color/md_theme_secondaryContainer"
android:id="@+id/QuickUnlockBlocked"
android:padding="16dp"
android:layout_gravity="center">
<TextView
android:id="@+id/quick_unlock_blocked_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/password_based_quick_unlock_not_available"
android:textSize="16sp"
android:textStyle="bold"
android:gravity="center"
android:paddingBottom="8dp"/>
<TextView
android:id="@+id/alert_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/password_based_quick_unlock_not_available_text"
android:textSize="16sp"
android:paddingBottom="8dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/QuickUnlock_buttonEnableLock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:backgroundTint="@color/md_theme_secondary"
android:textColor="@android:color/white"
android:text="@string/enable_screen_lock"
android:fontFamily="sans-serif-medium" />
<Button
android:id="@+id/QuickUnlock_buttonCloseDb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:backgroundTint="@color/md_theme_secondary"
android:textColor="@android:color/white"
android:fontFamily="sans-serif-medium"
android:text="@string/QuickUnlock_lockButton" />
</LinearLayout>
</LinearLayout>
<View <View
android:id="@+id/spacing" android:id="@+id/spacing"

View File

@@ -93,6 +93,8 @@
<string name="disable_fingerprint_unlock">Disable Biometric Unlock</string> <string name="disable_fingerprint_unlock">Disable Biometric Unlock</string>
<string name="enable_fingerprint_unlock">Enable full Biometric Unlock</string> <string name="enable_fingerprint_unlock">Enable full Biometric Unlock</string>
<string name="enable_fingerprint_quickunlock">Enable Biometric Unlock for QuickUnlock</string> <string name="enable_fingerprint_quickunlock">Enable Biometric Unlock for QuickUnlock</string>
<string name="password_based_quick_unlock_not_available">Password-based QuickUnlock not available</string>
<string name="password_based_quick_unlock_not_available_text">QuickUnlock using a part of your password is blocked because screen lock is not activated on your device. This behavior is to protect you in case somebody watched you entering your QuickUnlock key.</string>
<string name="fingerprint_unlock_failed">Biometric Unlock failed. Decryption key was invalidated by Android OS. This usually happens if a biometric authentication or security settings were changed. </string> <string name="fingerprint_unlock_failed">Biometric Unlock failed. Decryption key was invalidated by Android OS. This usually happens if a biometric authentication or security settings were changed. </string>
<string name="fingerprint_disabled_wrong_masterkey">Unlocking the database failed: Invalid composite key. Biometric Unlock was disabled because apparently the stored master password is no longer valid. </string> <string name="fingerprint_disabled_wrong_masterkey">Unlocking the database failed: Invalid composite key. Biometric Unlock was disabled because apparently the stored master password is no longer valid. </string>
<string name="fingerprint_reenable">Please re-enable Biometric Unlock for the new master password.</string> <string name="fingerprint_reenable">Please re-enable Biometric Unlock for the new master password.</string>
@@ -319,6 +321,7 @@
<string name="QuickUnlock_label_secure">Enter QuickUnlock code:</string> <string name="QuickUnlock_label_secure">Enter QuickUnlock code:</string>
<string name="QuickUnlock_button">QuickUnlock!</string> <string name="QuickUnlock_button">QuickUnlock!</string>
<string name="QuickUnlock_lockButton">Close database</string> <string name="QuickUnlock_lockButton">Close database</string>
<string name="enable_screen_lock">Enable screen lock</string>
<string name="QuickUnlockDefaultEnabled_title">Enable QuickUnlock by default</string> <string name="QuickUnlockDefaultEnabled_title">Enable QuickUnlock by default</string>
<string name="QuickUnlockDefaultEnabled_summary">Defines whether QuickUnlock is enabled by default or not.</string> <string name="QuickUnlockDefaultEnabled_summary">Defines whether QuickUnlock is enabled by default or not.</string>
<string name="ViewDatabaseSecure_title">Protect database display</string> <string name="ViewDatabaseSecure_title">Protect database display</string>

View File

@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<network-security-config> <network-security-config>
<base-config cleartextTrafficPermitted="true"> <base-config>
<trust-anchors> <trust-anchors>
<certificates src="system" /> <certificates src="system" />
<certificates src="user" /> <certificates src="user" />

View File

@@ -351,6 +351,9 @@ namespace keepass2android
QuickUnlockEnabled = enabled; QuickUnlockEnabled = enabled;
} }
public bool ScreenLockWasEnabledWhenOpeningDatabase { get; set; }
public bool QuickUnlockEnabled { get; private set; } public bool QuickUnlockEnabled { get; private set; }
public int QuickUnlockKeyLength { get; private set; } public int QuickUnlockKeyLength { get; private set; }