diff --git a/lib/SimpleSAML/Store/SQL.php b/lib/SimpleSAML/Store/SQL.php
index b555e46cec4520e44744efadbca4a8b618691f0b..7b837c0644b1e1cd68e5029471f14b456e3ca3c8 100644
--- a/lib/SimpleSAML/Store/SQL.php
+++ b/lib/SimpleSAML/Store/SQL.php
@@ -306,6 +306,10 @@ class SimpleSAML_Store_SQL extends SimpleSAML_Store {
 		assert('is_string($type)');
 		assert('is_string($key)');
 
+		if (strlen($key) > 50) {
+			$key = sha1($key);
+		}
+
 		$data = array(
 			'_type' => $type,
 			'_key' => $key,