예제 #1
0
        public static string SaltPassword(string password, byte[] salt = null)
        {
            if (salt == null)
            {
                salt = Secure.Rand(PasswordSaltSize);
            }

            byte[] pw  = password.NonNull().GetBytes_UTF8();
            byte[] src = pw;

            for (int i = 0; i < PasswordIterations; i++)
            {
                src = Secure.HashSHA256(src.CombineByte(salt));
            }

            return(src.CombineByte(salt).GetHexString());
        }
예제 #2
0
        // PKCS パディング
        public static byte[] PkcsPadding(byte[] srcData, int destSize)
        {
            int srcSize = srcData.Length;

            if ((srcSize + 11) > destSize)
            {
                throw new OverflowException();
            }

            int randSize = destSize - srcSize - 3;

            byte[] rand = Secure.Rand((uint)randSize);

            Buf b = new Buf();

            b.WriteByte(0x00);
            b.WriteByte(0x02);
            b.Write(rand);
            b.WriteByte(0x00);
            b.Write(srcData);

            return(b.ByteData);
        }
예제 #3
0
        // 初期化
        static Env()
        {
            FrameworkVersion = Environment.Version;
            if (FrameworkInfoString.StartsWith(".NET Core", StringComparison.InvariantCultureIgnoreCase))
            {
                IsDotNetCore = true;
            }
            OsInfo    = Environment.OSVersion;
            IsWindows = (OsInfo.Platform == PlatformID.Win32NT);
            if (IsUnix)
            {
                if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
                {
                    IsLinux = true;
                }
                else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
                {
                    IsMac = true;
                }
            }

            PathSeparator = "" + Path.DirectorySeparatorChar;
            if (Str.IsEmptyStr(PathSeparator))
            {
                PathSeparator = "/";
                if (Environment.OSVersion.Platform == PlatformID.Win32NT)
                {
                    PathSeparator = "\\";
                }
            }
            ExeFileName = IO.RemoveLastEnMark(getMyExeFileName());
            if (Str.IsEmptyStr(ExeFileName) == false)
            {
                AppRootDir = ExeFileDir = IO.RemoveLastEnMark(System.AppContext.BaseDirectory);
                // プログラムのあるディレクトリから 1 つずつ遡ってアプリケーションの root ディレクトリを取得する
                string tmp = ExeFileDir;
                while (true)
                {
                    try
                    {
                        tmp = Path.GetDirectoryName(tmp);
                        if (File.Exists(Path.Combine(tmp, "approot")) || File.Exists(Path.Combine(tmp, "appsettings.json")) || File.Exists(Path.Combine(tmp, "appsettings.Development.json")))
                        {
                            AppRootDir = tmp;
                            break;
                        }
                    }
                    catch
                    {
                        break;
                    }
                }
            }
            else
            {
                ExeFileName = "/tmp/dummyexe";
                ExeFileDir  = "/tmp";
                AppRootDir  = IO.RemoveLastEnMark(Environment.CurrentDirectory);
            }
            HomeDir = IO.RemoveLastEnMark(Kernel.GetEnvStr("HOME"));
            if (Str.IsEmptyStr(HomeDir))
            {
                HomeDir = IO.RemoveLastEnMark(Kernel.GetEnvStr("HOMEDRIVE") + Kernel.GetEnvStr("HOMEPATH"));
            }
            if (Str.IsEmptyStr(HomeDir) == false)
            {
                UnixMutantDir = Path.Combine(HomeDir, ".dnmutant");
            }
            else
            {
                HomeDir = AppRootDir;
                if (IsUnix)
                {
                    UnixMutantDir = Path.Combine("/tmp", ".dnmutant");
                }
            }
            if (IsWindows)
            {
                UnixMutantDir = "";
            }
            if (Str.IsEmptyStr(UnixMutantDir) == false)
            {
                IO.MakeDirIfNotExists(UnixMutantDir);
            }
            if (IsWindows)
            {
                // Windows
                SystemDir  = IO.RemoveLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.System));
                WindowsDir = IO.RemoveLastEnMark(Path.GetDirectoryName(SystemDir));
                TempDir    = IO.RemoveLastEnMark(Path.GetTempPath());
                WinTempDir = IO.RemoveLastEnMark(Path.Combine(WindowsDir, "Temp"));
                IO.MakeDir(WinTempDir);
                if (WindowsDir.Length >= 2 && WindowsDir[1] == ':')
                {
                    WindowsDir = WindowsDir.Substring(0, 2).ToUpper();
                }
                else
                {
                    WindowsDrive = "C:";
                }
            }
            else
            {
                // UNIX
                SystemDir    = "/bin";
                WindowsDir   = "/bin";
                WindowsDrive = "/";
                if (Str.IsEmptyStr(HomeDir) == false)
                {
                    TempDir = Path.Combine(HomeDir, ".dntmp");
                }
                else
                {
                    TempDir = "/tmp";
                }
                WinTempDir = TempDir;
            }
            ProgramFilesDir      = IO.RemoveLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles));
            PersonalStartMenuDir = IO.RemoveLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.StartMenu));
            PersonalProgramsDir  = IO.RemoveLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.Programs));
            PersonalStartupDir   = IO.RemoveLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.Startup));
            PersonalAppDataDir   = IO.RemoveLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
            PersonalDesktopDir   = IO.RemoveLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory));
            MyDocumentsDir       = IO.RemoveLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments));
            LocalAppDataDir      = IO.RemoveLastEnMark(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
            if (IsUnix)
            {
                // ダミーディレクトリ
                SystemDir            = "/bin";
                WindowsDir           = "/bin";
                WindowsDrive         = "/";
                ProgramFilesDir      = "/bin";
                PersonalStartMenuDir = Path.Combine(HomeDir, "dummy/starmenu");
                PersonalProgramsDir  = Path.Combine(HomeDir, "dummy/starmenu/programs");
                PersonalStartupDir   = Path.Combine(HomeDir, "dummy/starmenu/startup");
                LocalAppDataDir      = PersonalAppDataDir = Path.Combine(HomeDir, ".dnappdata");
                PersonalDesktopDir   = Path.Combine(HomeDir, "dummy/desktop");
                MyDocumentsDir       = HomeDir;
            }
            StartupCurrentDir = CurrentDir;
            UserName          = Environment.UserName;
            try
            {
                UserNameEx = Environment.UserDomainName + "\\" + UserName;
            }
            catch
            {
                UserNameEx = UserName;
            }
            MachineName    = Environment.MachineName;
            CommandLine    = initCommandLine(Environment.CommandLine);
            IsLittleEndian = BitConverter.IsLittleEndian;
            ProcessId      = System.Diagnostics.Process.GetCurrentProcess().Id;
            IsAdmin        = checkIsAdmin();

            // 自分用の temp ディレクトリの初期化
            try
            {
                deleteUnusedTempDir();
            }
            catch
            {
            }

            int num = 0;

            while (true)
            {
                byte[] rand = Secure.Rand(2);
                string tmp2 = Str.ByteToStr(rand);

                string tmp = Path.Combine(Env.TempDir, "NET_" + tmp2);

                if (IO.IsDirExists(tmp) == false && IO.MakeDir(tmp))
                {
                    Env.MyTempDir = tmp;

                    break;
                }

                if ((num++) >= 100)
                {
                    throw new SystemException();
                }
            }

            if (IsWindows)
            {
                UnixMutantDir = Env.MyTempDir;
            }

            // ロックファイルの作成
            string lockFileName = Path.Combine(Env.MyTempDir, "LockFile.dat");

            lockFile = IO.FileCreate(lockFileName, Env.IsUnix);
        }