001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.plugin.column;
017
018import org.opengion.fukurou.security.HybsCryptography;
019import org.opengion.fukurou.util.StringUtil;
020import org.opengion.hayabusa.db.AbstractDBType;
021
022/**
023 * パスワード情報など、重要な情報のハッシュコード(MD5)を扱う為の、カラム属性を定義します。
024 *
025 * パスワード情報など、重要な情報のハッシュコードに、MD5 があります。このクラスは、
026 * MessageDigestにより、MD5 でハッシュした文字を作成します。
027 * 値としては、標準の X と同じ半角文字列「 c < 0x20 || c > 0x7e 以外」でのみ
028 * 処理することが出来ます。
029 *
030 * タイプチェックとして、以下の条件を判定します。
031 * ・文字列長は、Byte換算での文字数との比較
032 * ・半角文字列チェック「 c < 0x20 || c > 0x7e 以外」エラー
033 * ・文字パラメータの 正規表現チェック
034 * ・クロスサイトスクリプティングチェック
035 *
036 * @og.group データ属性
037 *
038 * @version  4.0
039 * @author   Kazuhiko Hasegawa
040 * @since    JDK5.0,
041 */
042public class DBType_MD5 extends AbstractDBType {
043        /** このプログラムのVERSION文字列を設定します。   {@value} */
044        private static final String VERSION = "8.1.2.0 (2022/03/10)" ;
045
046        /**
047         * デフォルトコンストラクター
048         *
049         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
050         */
051        public DBType_MD5() { super(); }                // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
052
053        /**
054         * String引数の文字列を+1した文字列を返します。
055         * ※ このクラスでは実装されていません。
056         *
057         * @param       value   String引数の文字列
058         * @throws UnsupportedOperationException このクラスを実行すると、必ず発生します。
059         *
060         * @return  String引数の文字列を+1した文字列
061         */
062        @Override
063        public String valueAdd( final String value ) {
064                final String errMsg = "このメソッドは、このクラスからは使用できません。";
065                throw new UnsupportedOperationException( errMsg );
066        }
067
068        /**
069         * MessageDigestにより、MD5 でハッシュした文字を返します。
070         *
071         * MD5で、15Byteのバイトに変換されますが、ここでは、マイナス時には,
072         * 符号を反転させて、16進数で文字列に変換しています。
073         * よって、このメソッドで変換した文字でのみ突き合わせて正しいかどうかを
074         * 判断してください。
075         *
076         * @og.rev 3.3.3.0 (2003/07/09) 前後のスペースを取り除いておく。
077         * @og.rev 3.3.3.1 (2003/07/18) 後ろスペースを取り除く。(StringUtil#rTrim)
078         * @og.rev 5.2.2.0 (2010/11/01) util.StringUtil から security.HybsCryptography へ移動
079         * @og.rev 8.1.2.0 (2022/03/10) getMD5 メソッドを getHash メソッドに変更
080         *
081         * @param       value   (一般に編集データとして登録されたデータ)
082         *
083         * @return  修正後の文字列(一般にデータベースに登録するデータ)
084         */
085        @Override
086        public String valueSet( final String value ) {
087//              return HybsCryptography.getMD5( StringUtil.rTrim( value ) );    // 5.2.2.0 (2010/11/01) クラス変更 8.1.2.0 (2022/03/10) Modify
088                return HybsCryptography.getHash( "MD5", StringUtil.rTrim( value ) );
089        }
090}