特定のJSON形式なのかどうかのvalidationをPHPで行う

ある変数の中身がJSON形式かどうかを判別したくなりました。当初、正規表現であれこれするのかと思ったのですが、何だか難しそうです。JSONでは様々な形を表現出来ますが、{“piyo”:[1,2,3,4,5]}みたいなのだけOK、それ以外のもの、たとえば{“piyo”:”fuga”}はだめ、という風にしたかったのですがうまい方法が見当たらず困っていたのですが、ナイーブな実装が出来たので書いておきます。

<?php
$json1 = '{"piyo":"fuga"}'; // ← 典型的なJSON
$json2 = '{"piyo":"fuga", "hoge":"fizz"}'; // ← key/valueのセットが2つ
$json3 = '{"piyo":[1,2,3,4,5]}'; // ← 配列を含んだJSON
$json4 = '[1,2,3,4,5]'; // ← 配列だけのJSON
$json5 = '{"piyo":"fuga"'; // ← JSON形式ではない文字列。「}」が抜けている。
$json6 = '{"piyo"[1,2,3,4,5]}'; // ← 同様に非JSON配列。「:」が抜けている。
$json7 = '{"doc_ids":["mario", "peach", "yoshi"]}'; // ←これを判別したい。

check_array_json($json1);
check_array_json($json2);
check_array_json($json3);
check_array_json($json4);
check_array_json($json5);
check_array_json($json6);
check_array_json($json7);

function check_array_json($json) {
	$temp = json_decode($json, true, 3);
	if (!$temp) {
		print "decode failed. \n";
	} else {
		print "decode success. ";
		//var_dump($temp);
		if (isset($temp["doc_ids"])) {
			foreach ($temp["doc_ids"] as $value) {
				//print $value . "\n";
			}
			print "GREAT!!!!";
		} else {
			print "Not ideal JSON format.\n";
		}
	}
}
?>

出力結果↓
decode success. Not ideal JSON format.
decode success. Not ideal JSON format.
decode success. Not ideal JSON format.
decode success. Not ideal JSON format.
decode failed. 
decode failed. 
decode success. GREAT!!!!

とこのように、$json7だけvalidなのが分かります。issetを組み合わせるのがキモでしょうか。JSONの弱いところは、validationなのではと思ったり。パースしてはじめて判断がつくのは何かと問題があるのでは。投機的実行と言いますか。。。とは言えJSON便利なので使いますが。

PHPでtimezone一覧を表示、そしてarrayに格納しやすい形として出力

PHPのtimezone一覧が欲しい!と思って調べてみたら、PHP Manualに載っていました。それは結構なのですが、PHPのarrayに格納したいのです。データ量が膨大で、一つ一つ整形していくと一生掛かりそうです。

なんとかならないかと探していたら、PHP Manualに載っていましたので、若干手を入れて、ソースと出力を載せておきます。出力をコピペしてarrayに入れて、ソースコード整形を押せば一発ですね!

コード

<?php

$timezone_identifiers = DateTimeZone::listIdentifiers();
for ($i = 0; $i < count($timezone_identifiers); $i++) {
	printf("%s%s%s,\n", '"', $timezone_identifiers[$i], '"');
}
?>

出力
"Africa/Abidjan",
"Africa/Accra",
"Africa/Addis_Ababa",
"Africa/Algiers",
"Africa/Asmara",
"Africa/Bamako",
"Africa/Bangui",
"Africa/Banjul",
"Africa/Bissau",
"Africa/Blantyre",
"Africa/Brazzaville",
"Africa/Bujumbura",
"Africa/Cairo",
"Africa/Casablanca",
"Africa/Ceuta",
"Africa/Conakry",
"Africa/Dakar",
"Africa/Dar_es_Salaam",
"Africa/Djibouti",
"Africa/Douala",
"Africa/El_Aaiun",
"Africa/Freetown",
"Africa/Gaborone",
"Africa/Harare",
"Africa/Johannesburg",
"Africa/Juba",
"Africa/Kampala",
"Africa/Khartoum",
"Africa/Kigali",
"Africa/Kinshasa",
"Africa/Lagos",
"Africa/Libreville",
"Africa/Lome",
"Africa/Luanda",
"Africa/Lubumbashi",
"Africa/Lusaka",
"Africa/Malabo",
"Africa/Maputo",
"Africa/Maseru",
"Africa/Mbabane",
"Africa/Mogadishu",
"Africa/Monrovia",
"Africa/Nairobi",
"Africa/Ndjamena",
"Africa/Niamey",
"Africa/Nouakchott",
"Africa/Ouagadougou",
"Africa/Porto-Novo",
"Africa/Sao_Tome",
"Africa/Tripoli",
"Africa/Tunis",
"Africa/Windhoek",
"America/Adak",
"America/Anchorage",
"America/Anguilla",
"America/Antigua",
"America/Araguaina",
"America/Argentina/Buenos_Aires",
"America/Argentina/Catamarca",
"America/Argentina/Cordoba",
"America/Argentina/Jujuy",
"America/Argentina/La_Rioja",
"America/Argentina/Mendoza",
"America/Argentina/Rio_Gallegos",
"America/Argentina/Salta",
"America/Argentina/San_Juan",
"America/Argentina/San_Luis",
"America/Argentina/Tucuman",
"America/Argentina/Ushuaia",
"America/Aruba",
"America/Asuncion",
"America/Atikokan",
"America/Bahia",
"America/Bahia_Banderas",
"America/Barbados",
"America/Belem",
"America/Belize",
"America/Blanc-Sablon",
"America/Boa_Vista",
"America/Bogota",
"America/Boise",
"America/Cambridge_Bay",
"America/Campo_Grande",
"America/Cancun",
"America/Caracas",
"America/Cayenne",
"America/Cayman",
"America/Chicago",
"America/Chihuahua",
"America/Costa_Rica",
"America/Creston",
"America/Cuiaba",
"America/Curacao",
"America/Danmarkshavn",
"America/Dawson",
"America/Dawson_Creek",
"America/Denver",
"America/Detroit",
"America/Dominica",
"America/Edmonton",
"America/Eirunepe",
"America/El_Salvador",
"America/Fortaleza",
"America/Glace_Bay",
"America/Godthab",
"America/Goose_Bay",
"America/Grand_Turk",
"America/Grenada",
"America/Guadeloupe",
"America/Guatemala",
"America/Guayaquil",
"America/Guyana",
"America/Halifax",
"America/Havana",
"America/Hermosillo",
"America/Indiana/Indianapolis",
"America/Indiana/Knox",
"America/Indiana/Marengo",
"America/Indiana/Petersburg",
"America/Indiana/Tell_City",
"America/Indiana/Vevay",
"America/Indiana/Vincennes",
"America/Indiana/Winamac",
"America/Inuvik",
"America/Iqaluit",
"America/Jamaica",
"America/Juneau",
"America/Kentucky/Louisville",
"America/Kentucky/Monticello",
"America/Kralendijk",
"America/La_Paz",
"America/Lima",
"America/Los_Angeles",
"America/Lower_Princes",
"America/Maceio",
"America/Managua",
"America/Manaus",
"America/Marigot",
"America/Martinique",
"America/Matamoros",
"America/Mazatlan",
"America/Menominee",
"America/Merida",
"America/Metlakatla",
"America/Mexico_City",
"America/Miquelon",
"America/Moncton",
"America/Monterrey",
"America/Montevideo",
"America/Montreal",
"America/Montserrat",
"America/Nassau",
"America/New_York",
"America/Nipigon",
"America/Nome",
"America/Noronha",
"America/North_Dakota/Beulah",
"America/North_Dakota/Center",
"America/North_Dakota/New_Salem",
"America/Ojinaga",
"America/Panama",
"America/Pangnirtung",
"America/Paramaribo",
"America/Phoenix",
"America/Port-au-Prince",
"America/Port_of_Spain",
"America/Porto_Velho",
"America/Puerto_Rico",
"America/Rainy_River",
"America/Rankin_Inlet",
"America/Recife",
"America/Regina",
"America/Resolute",
"America/Rio_Branco",
"America/Santa_Isabel",
"America/Santarem",
"America/Santiago",
"America/Santo_Domingo",
"America/Sao_Paulo",
"America/Scoresbysund",
"America/Shiprock",
"America/Sitka",
"America/St_Barthelemy",
"America/St_Johns",
"America/St_Kitts",
"America/St_Lucia",
"America/St_Thomas",
"America/St_Vincent",
"America/Swift_Current",
"America/Tegucigalpa",
"America/Thule",
"America/Thunder_Bay",
"America/Tijuana",
"America/Toronto",
"America/Tortola",
"America/Vancouver",
"America/Whitehorse",
"America/Winnipeg",
"America/Yakutat",
"America/Yellowknife",
"Antarctica/Casey",
"Antarctica/Davis",
"Antarctica/DumontDUrville",
"Antarctica/Macquarie",
"Antarctica/Mawson",
"Antarctica/McMurdo",
"Antarctica/Palmer",
"Antarctica/Rothera",
"Antarctica/South_Pole",
"Antarctica/Syowa",
"Antarctica/Vostok",
"Arctic/Longyearbyen",
"Asia/Aden",
"Asia/Almaty",
"Asia/Amman",
"Asia/Anadyr",
"Asia/Aqtau",
"Asia/Aqtobe",
"Asia/Ashgabat",
"Asia/Baghdad",
"Asia/Bahrain",
"Asia/Baku",
"Asia/Bangkok",
"Asia/Beirut",
"Asia/Bishkek",
"Asia/Brunei",
"Asia/Choibalsan",
"Asia/Chongqing",
"Asia/Colombo",
"Asia/Damascus",
"Asia/Dhaka",
"Asia/Dili",
"Asia/Dubai",
"Asia/Dushanbe",
"Asia/Gaza",
"Asia/Harbin",
"Asia/Hebron",
"Asia/Ho_Chi_Minh",
"Asia/Hong_Kong",
"Asia/Hovd",
"Asia/Irkutsk",
"Asia/Jakarta",
"Asia/Jayapura",
"Asia/Jerusalem",
"Asia/Kabul",
"Asia/Kamchatka",
"Asia/Karachi",
"Asia/Kashgar",
"Asia/Kathmandu",
"Asia/Kolkata",
"Asia/Krasnoyarsk",
"Asia/Kuala_Lumpur",
"Asia/Kuching",
"Asia/Kuwait",
"Asia/Macau",
"Asia/Magadan",
"Asia/Makassar",
"Asia/Manila",
"Asia/Muscat",
"Asia/Nicosia",
"Asia/Novokuznetsk",
"Asia/Novosibirsk",
"Asia/Omsk",
"Asia/Oral",
"Asia/Phnom_Penh",
"Asia/Pontianak",
"Asia/Pyongyang",
"Asia/Qatar",
"Asia/Qyzylorda",
"Asia/Rangoon",
"Asia/Riyadh",
"Asia/Sakhalin",
"Asia/Samarkand",
"Asia/Seoul",
"Asia/Shanghai",
"Asia/Singapore",
"Asia/Taipei",
"Asia/Tashkent",
"Asia/Tbilisi",
"Asia/Tehran",
"Asia/Thimphu",
"Asia/Tokyo",
"Asia/Ulaanbaatar",
"Asia/Urumqi",
"Asia/Vientiane",
"Asia/Vladivostok",
"Asia/Yakutsk",
"Asia/Yekaterinburg",
"Asia/Yerevan",
"Atlantic/Azores",
"Atlantic/Bermuda",
"Atlantic/Canary",
"Atlantic/Cape_Verde",
"Atlantic/Faroe",
"Atlantic/Madeira",
"Atlantic/Reykjavik",
"Atlantic/South_Georgia",
"Atlantic/St_Helena",
"Atlantic/Stanley",
"Australia/Adelaide",
"Australia/Brisbane",
"Australia/Broken_Hill",
"Australia/Currie",
"Australia/Darwin",
"Australia/Eucla",
"Australia/Hobart",
"Australia/Lindeman",
"Australia/Lord_Howe",
"Australia/Melbourne",
"Australia/Perth",
"Australia/Sydney",
"Europe/Amsterdam",
"Europe/Andorra",
"Europe/Athens",
"Europe/Belgrade",
"Europe/Berlin",
"Europe/Bratislava",
"Europe/Brussels",
"Europe/Bucharest",
"Europe/Budapest",
"Europe/Chisinau",
"Europe/Copenhagen",
"Europe/Dublin",
"Europe/Gibraltar",
"Europe/Guernsey",
"Europe/Helsinki",
"Europe/Isle_of_Man",
"Europe/Istanbul",
"Europe/Jersey",
"Europe/Kaliningrad",
"Europe/Kiev",
"Europe/Lisbon",
"Europe/Ljubljana",
"Europe/London",
"Europe/Luxembourg",
"Europe/Madrid",
"Europe/Malta",
"Europe/Mariehamn",
"Europe/Minsk",
"Europe/Monaco",
"Europe/Moscow",
"Europe/Oslo",
"Europe/Paris",
"Europe/Podgorica",
"Europe/Prague",
"Europe/Riga",
"Europe/Rome",
"Europe/Samara",
"Europe/San_Marino",
"Europe/Sarajevo",
"Europe/Simferopol",
"Europe/Skopje",
"Europe/Sofia",
"Europe/Stockholm",
"Europe/Tallinn",
"Europe/Tirane",
"Europe/Uzhgorod",
"Europe/Vaduz",
"Europe/Vatican",
"Europe/Vienna",
"Europe/Vilnius",
"Europe/Volgograd",
"Europe/Warsaw",
"Europe/Zagreb",
"Europe/Zaporozhye",
"Europe/Zurich",
"Indian/Antananarivo",
"Indian/Chagos",
"Indian/Christmas",
"Indian/Cocos",
"Indian/Comoro",
"Indian/Kerguelen",
"Indian/Mahe",
"Indian/Maldives",
"Indian/Mauritius",
"Indian/Mayotte",
"Indian/Reunion",
"Pacific/Apia",
"Pacific/Auckland",
"Pacific/Chatham",
"Pacific/Chuuk",
"Pacific/Easter",
"Pacific/Efate",
"Pacific/Enderbury",
"Pacific/Fakaofo",
"Pacific/Fiji",
"Pacific/Funafuti",
"Pacific/Galapagos",
"Pacific/Gambier",
"Pacific/Guadalcanal",
"Pacific/Guam",
"Pacific/Honolulu",
"Pacific/Johnston",
"Pacific/Kiritimati",
"Pacific/Kosrae",
"Pacific/Kwajalein",
"Pacific/Majuro",
"Pacific/Marquesas",
"Pacific/Midway",
"Pacific/Nauru",
"Pacific/Niue",
"Pacific/Norfolk",
"Pacific/Noumea",
"Pacific/Pago_Pago",
"Pacific/Palau",
"Pacific/Pitcairn",
"Pacific/Pohnpei",
"Pacific/Port_Moresby",
"Pacific/Rarotonga",
"Pacific/Saipan",
"Pacific/Tahiti",
"Pacific/Tarawa",
"Pacific/Tongatapu",
"Pacific/Wake",
"Pacific/Wallis",
"UTC",

PHPでSHA-3のsaltとstretchingと時間など

ユーザーのパスワードは、DBに突っ込むときにはhash化しておく必要がありますね。
saltを加えてstretchですね。SHA-3つまりkeccakで書いてみてました(PHPでkeccakを使う方法はこちら->http://mitsuakikawamorita.com/blog/?p=1018)。さらに時間を比較してみました。

本当はコードはgithubに上げて表にしてグラフにするのが正義なんでしょうけれど、面倒なのでやめておきます(*ノω・*)

コード↓

Utils.php

<?php
class Utils {
    static function do_keccak_stretching($string, $salt, $num)
    {
        $hash = '';
        for ($i = 0; $i < $num; $i++) {
            $hash = bin2hex(keccak_hash($hash . $string . $salt));
        }
        return $hash;
    }
}
?>

sandbox.php
<?php
require './Utils.php';
function hash_time($num) {
    $start = microtime(true);
    echo $num . " times stretching: " . Utils::do_keccak_stretching("piyopiyo", "salt", $num) . "\n";
    $end = microtime(true);
    printf("%8d times: %.4f sec\n", $num, (double) ($end - $start));
    printf("-------------------------\n");
}
hash_time(1);
hash_time(10);
hash_time(50);
hash_time(100);
hash_time(500);
hash_time(1000);
hash_time(5000);
hash_time(10000);
hash_time(50000);
hash_time(100000);
hash_time(500000);
hash_time(1000000);
?>

として、
php sandbox.php
とすればいいだけです。

MacBook late2009(core2duo, 4GBDRAM, Mac OS 10.6 Snow Leopard) ↓
$ php -v
PHP 5.4.10 (cli) (built: Dec 20 2012 19:20:33) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
    with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans

$ php sandbox.php 
1 times stretching: 2b6302003a8692caf9f21aa88435d8b8f42feb60c8deaa0ba3cf7137f2bac69c2818a15a2a8d7a56355a0cf694c8212330b9a148d7cfe879f3fce984fb782aa4
       1 times: 0.0003 sec
-------------------------
10 times stretching: 0f2ec055845fdfa2e06124c4e14c3712a0f7f57de4a66a1141106c1cb61c0851ad8a60de03dce678425b7fb86ce952358042f2485023833f294b1714b72aa0a6
      10 times: 0.0001 sec
-------------------------
50 times stretching: 284010b3b3547f1386b5bfb37096d1eaa36556b4a215e4303c7a649afa72aed26a8897df03866babbb3ec286319e9621a5d7547c16286f37adbdf09bb2b0cae9
      50 times: 0.0005 sec
-------------------------
100 times stretching: f9b5966ac33b652bd26809031e1693ee4bd772674fa2de1b9756e9c7406753e1da3cb9e10ed2b0a31f5b8b5270cad9ebb506e4daacb3ae1b7a3e35f3afa97633
     100 times: 0.0018 sec
-------------------------
500 times stretching: 0e5979b15c3892faf3843640ec4e24b9e81eb14731317a83d27d88f21d6151bc6c719a1f056ab8022df7a667b952b7bdf85838f3eb1044fb5074c06eb015f800
     500 times: 0.0051 sec
-------------------------
1000 times stretching: fca0c29cb4365490f6e061319d7b57691e568dcf8846c234514b7f2a9cad44bcfd577c6cf146ac3fdae6d6f5fc71f787f251816c85a5d26d04a34913a7ff60ed
    1000 times: 0.0093 sec
-------------------------
5000 times stretching: 2ab861d5c16018cb67398fc6d16de7344f8e39aa04d1f05ac0d1d9e2370dc9e5381cc01ffa90a7728546bae00cf64e4bc329139b56292f49083ae130b2fd9473
    5000 times: 0.0501 sec
-------------------------
10000 times stretching: 6355ada3056e23322fd3c6211faeb105dfb2d7739041b96bf496231bc516296bc2bcd29f53f5339067df7b02d39d2b722e8bf0f45f25c28deda97845aca8574a
   10000 times: 0.0936 sec
-------------------------
50000 times stretching: c7a0261ea2141e89264380e85803ef5f254200382eb15c9e279a3b61d2f11437409686c0e77f2f80d01d833cf8c92d9c9897c3ab2113a849848f71436ef48140
   50000 times: 0.4663 sec
-------------------------
100000 times stretching: bd1d0adb655bd68d01d2f8a6e8422f2092f0f2aace3946b4f65060e190ab7defa479af60f7b29770e2fcf2970072e69f3b209f38ec12578fc00d682bc733dfe6
  100000 times: 0.9269 sec
-------------------------
500000 times stretching: c0b3c88db255784ec358507410acfeec1ce3388510a667764b532c6a52dbba53547eb70ce81d9b411fe3be97eed21d669774c8377532d53e47563c5ef4e0f1d3
  500000 times: 4.6296 sec
-------------------------
1000000 times stretching: fe929fdddc2bc1f63ea7f0b7500eee2542a4c0a56a98781fd4c4571295b6497e4d1d0006fcb7bbcfcea38910070f39362751c9c0c8cac2233a7f5ff08ade528d
 1000000 times: 9.2597 sec
-------------------------


とあるVM
– CPU 2cores (Intel(R)Xeon(R) CPU X5650 @ 2.67GHz)
– 4GBDRAM (“dd if=/dev/zero of=/dev/null bs=1024K count=100000” したら、11.3 GB/s でした)
$ php -v
PHP 5.3.3 (cli) (built: Jul  3 2012 16:53:21) 
Copyright (c) 1997-2010 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2010 Zend Technologies

$ php sandbox.php 
1 times stretching: 2b6302003a8692caf9f21aa88435d8b8f42feb60c8deaa0ba3cf7137f2bac69c2818a15a2a8d7a56355a0cf694c8212330b9a148d7cfe879f3fce984fb782aa4
       1 times: 0.0001 sec
-------------------------
10 times stretching: 0f2ec055845fdfa2e06124c4e14c3712a0f7f57de4a66a1141106c1cb61c0851ad8a60de03dce678425b7fb86ce952358042f2485023833f294b1714b72aa0a6
      10 times: 0.0000 sec
-------------------------
50 times stretching: 284010b3b3547f1386b5bfb37096d1eaa36556b4a215e4303c7a649afa72aed26a8897df03866babbb3ec286319e9621a5d7547c16286f37adbdf09bb2b0cae9
      50 times: 0.0001 sec
-------------------------
100 times stretching: f9b5966ac33b652bd26809031e1693ee4bd772674fa2de1b9756e9c7406753e1da3cb9e10ed2b0a31f5b8b5270cad9ebb506e4daacb3ae1b7a3e35f3afa97633
     100 times: 0.0002 sec
-------------------------
500 times stretching: 0e5979b15c3892faf3843640ec4e24b9e81eb14731317a83d27d88f21d6151bc6c719a1f056ab8022df7a667b952b7bdf85838f3eb1044fb5074c06eb015f800
     500 times: 0.0013 sec
-------------------------
1000 times stretching: fca0c29cb4365490f6e061319d7b57691e568dcf8846c234514b7f2a9cad44bcfd577c6cf146ac3fdae6d6f5fc71f787f251816c85a5d26d04a34913a7ff60ed
    1000 times: 0.0024 sec
-------------------------
5000 times stretching: 2ab861d5c16018cb67398fc6d16de7344f8e39aa04d1f05ac0d1d9e2370dc9e5381cc01ffa90a7728546bae00cf64e4bc329139b56292f49083ae130b2fd9473
    5000 times: 0.0112 sec
-------------------------
10000 times stretching: 6355ada3056e23322fd3c6211faeb105dfb2d7739041b96bf496231bc516296bc2bcd29f53f5339067df7b02d39d2b722e8bf0f45f25c28deda97845aca8574a
   10000 times: 0.0234 sec
-------------------------
50000 times stretching: c7a0261ea2141e89264380e85803ef5f254200382eb15c9e279a3b61d2f11437409686c0e77f2f80d01d833cf8c92d9c9897c3ab2113a849848f71436ef48140
   50000 times: 0.1537 sec
-------------------------
100000 times stretching: bd1d0adb655bd68d01d2f8a6e8422f2092f0f2aace3946b4f65060e190ab7defa479af60f7b29770e2fcf2970072e69f3b209f38ec12578fc00d682bc733dfe6
  100000 times: 0.2230 sec
-------------------------
500000 times stretching: c0b3c88db255784ec358507410acfeec1ce3388510a667764b532c6a52dbba53547eb70ce81d9b411fe3be97eed21d669774c8377532d53e47563c5ef4e0f1d3
  500000 times: 1.1210 sec
-------------------------
1000000 times stretching: fe929fdddc2bc1f63ea7f0b7500eee2542a4c0a56a98781fd4c4571295b6497e4d1d0006fcb7bbcfcea38910070f39362751c9c0c8cac2233a7f5ff08ade528d
 1000000 times: 2.2090 sec
-------------------------

Macではだいぶ遅いですね。どういうことでしょう。1万回やるなんて議論もありますが、SHA-3なのでストレッチングは100回ぐらいでいいのではないでしょうか。というかDB鯖に侵入されたらほぼ何もかも終わりのように思いますが。

個人用MongoDBチートシート

毎度忘れるので。。。

# 立ちあげ
[piyo@macbook: ~/my_repos/softwares/mongodb-osx-x86_64-2.2.2]
$ ./bin/mongod

# シェル
[piyo@macbook: ~/my_repos/softwares/mongodb-osx-x86_64-2.2.2]
$ ./bin/mongo
MongoDB shell version: 2.2.2
connecting to: test

# DB一覧
> show dbs;
fuga	0.203125GB
local	(empty)
test	0.203125GB

# DB切り替え
> use fuga
switched to db fuga

# ドキュメント数を表示
> db.users.count();
35

# ドキュメントを全部削除
> db.users.remove();

# オブジェクトid(_id)で検索
> db.users.find({"_id":ObjectId("50fb975f6d33d35103000002a")});

# サイズを表示
> db.users.stats()

# indexを貼る
> db.users.ensureIndex({"user_name":1});

# indexの確認
> db.users.getIndexes();

PHP 操作編

内部id(_idって表示されていて、ObjectIdなどと称されるもの)をキーに検索するサンプル
<?php

$link = new Mongo('localhost:27017');
$db = $link->buzz;
$col1 = $db->posts;

$document_id = "510232ac6d33d3f157000051";

if($col1->findOne(array("_id" => new MongoId($document_id))))
{
	echo "あるよ!";
} else {
	echo "ないよ!";
}
?>

MacのPHPでSHA-3(keccak)を使ってみる

SHA-2はオワコン!時代はケチャック!というわけでやってみました。

githubでSHA-3を実装したレポジトリがありましたのでありがたく使わせてもらいます。

MacPortsでインストールしたphp54に入れてみてます。

$ git clone https://github.com/jedisct1/keccak-php.git
$ cd keccak-php/
$ phpize54
$ ./configure --enable-keccak
$ sudo make install

$ sudo vi /opt/local/etc/php54/php.ini
# ↑ extension=keccak.so を書き加える

# ↓反映されているかどうか確認。keccakが表示されればOK
$ php54 -m | grep keccak
keccak

# ↓走らせてみます。
$ php54 -r 'echo bin2hex(keccak_hash("keccak!"));'
57b06e31a24ec64c5c04447142584c273ed2adaa85df181da9ec484feb3dd69474ef85315c67bbcfd874d36bb27c2dfd4c80ae0f6cd0b658d3778480588d4cb2